繁体   English   中英

剪贴板上有哪些信息? 使用 Python 3、win32剪贴板

[英]What information is located on the clipboard? Using Python 3, win32clipboard

最近我想写一个类似同步剪贴板的东西。 所以我使用 pywin32 中的 win32clipboard 在 python 3上编写了这段代码,以查看剪贴板上的内容。

'''

import win32clipboard as wn

def get_buffer():
    data = {}
    wn.OpenClipboard()
    next_format = wn.EnumClipboardFormats(0)
    while next_format:
        try:
            data[next_format] = wn.GetClipboardData(next_format)
        except:
            print("LOSE FORMAT", next_format)
        next_format = wn.EnumClipboardFormats(next_format)
    wn.CloseClipboard()
    return data

data = get_buffer()
for now_format in data:
    print(now_format, type(data[now_format]), data[now_format])

例如,如果您只从记事本复制文本:“Hello world” ,那么您会得到:

13 <class 'str'> Hello world
16 <class 'bytes'> b'\t\x04\x00\x00'
1 <class 'bytes'> b'Hello world'
7 <class 'bytes'> b'Hello world'

但是当你尝试更复杂的东西时,比如从桌面复制文件,你会得到:

LOSE FORMAT 49326
49161 <class 'bytes'> b'"\x02 \x00\x00\x00\x00\x00'
49470 <class 'bytes'> b"\x01\x00\x00\x00\x0c\x00\x00\x00\x0e\x00\x00\x00\x00\x00h\...
49672 <class 'bytes'> b'\x7f\x00D w\x00@\x00\x01\x00\x00\x00'
49673 <class 'bytes'> b'0\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00'
49490 <class 'bytes'> b'\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x...
15 <class 'tuple'> ('C:\\Users\\Public\\Desktop\\Unity Hub.lnk',)
49158 <class 'bytes'> b'C:\\Users\\Public\\Desktop\\UNITYH~1.LNK\x00'
49159 <class 'bytes'> b'C\x00:\x00\\\x00U\x00s\x00e\x00r\x00s\x00\\\x00P\x00u\x00b...
49366 <class 'bytes'> b'\x01\x00\x00\x00l\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\...
49493 <class 'bytes'> b'\x01\x00\x00\x00'
49471 <class 'bytes'> b'\xe5\x01\x00\x00o\x03\x00\x00\xe8\xff\xff\xff\xe0\xff\xff\...
49476 <class 'bytes'> b'\x05\x00\x00\x00'
49492 <class 'bytes'> b'\x01\x00\x00\x00'
49171 <class 'bytes'> b'\x00\x00\x00\x00\xe8\x02\x00\x00\x01\x00\x00\x00\r\x00\x00...

或类似从油漆复制图像

LOSE FORMAT 3
49161 <class 'bytes'> b'\xaa\nn\x00\x00\x00\x00\x00'
49163 <class 'bytes'> b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x...
49156 <class 'bytes'> b'BM\x06\x80\x01\x00\x00\x00\x00\x006\x00\x00\x00(\x00\x00\x...
49155 <class 'bytes'> b'Paint.Picture\x00\xc8\xe7\xee\xe1\xf0\xe0\xe6\xe5\xed\xe8\...
49166 <class 'bytes'> b'\x90\x00\x00\x00!K\xe3\xd3u\x9d\x1a\x10\x8c=\x00\xaa\x00\x...
8 <class 'bytes'> b'(\x00\x00\x00\xb7\x00\x00\x00\xb2\x00\x00\x00\x01\x00\x18\...
49171 <class 'bytes'> b'\x00\x00\x00\x00\x08\x01\x00\x00\x01\x00\x00\x00\x04\x00\x...
14 <class 'bytes'> b'\x01\x00\x00\x00l\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\...
2 <class 'int'> -821748004
17 <class 'bytes'> b'|\x00\x00\x00\xb7\x00\x00\x00\xb2\x00\x00\x00\x01\x00\x18\...

如此庞大的信息量意味着什么? 为什么我会丢失信息? 有人可以解释为什么这么多东西被复制以及这一切意味着什么吗? 我在哪里可以找到有关剪贴板上使用的格式的信息?

在 Microsoft 文档中,剪贴板格式分为三类:标准格式、注册格式和私有格式。

标准格式是由系统定义供任何程序使用的文本或图像之类的东西。 在您的示例中,格式 3 定义为CF_METAFILEPICT

METAFILEPICT结构定义的元文件图片格式的句柄。 当通过 DDE 传递 CF_METAFILEPICT 句柄时,负责删除 hMem 的应用程序还应该释放 CF_METAFILEPICT 句柄引用的元文件。

注册格式是特定程序根据自己的需要注册的格式。 这些使用0xC0000xFFFF范围内的值。 以十进制表示,即 49152 到 65535。因此,您必须查阅特定程序的文档以了解它们是什么以及如何阅读它们。

私有格式供特定应用程序内部私有使用。 这些使用从0x02000x02FF (512 到 767)的值。 你的例子都不在这个范围内,所以我假设你没有遇到过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM