Standard Clipboard Formats
The clipboard formats defined by the system are called standard clipboard formats.
Constant/value | Description |
---|---|
CF_BITMAP 2 | A handle to a bitmap (HBITMAP). |
CF_DIB 8 | A memory object containing a BITMAPINFO structure followed by the bitmap bits. |
CF_DIBV5 17 | A memory object containing a BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits. |
CF_DIF 5 | Software Arts' Data Interchange Format. |
CF_DSPBITMAP 0x0082 | Bitmap display format associated with a private format. ThehMem parameter must be a handle to data that can be displayed in bitmap format in lieu of the privately formatted data. |
CF_DSPENHMETAFILE 0x008E | Enhanced metafile display format associated with a private format. The hMem parameter must be a handle to data that can be displayed in enhanced metafile format in lieu of the privately formatted data. |
CF_DSPMETAFILEPICT 0x0083 | Metafile-picture display format associated with a private format. The hMem parameter must be a handle to data that can be displayed in metafile-picture format in lieu of the privately formatted data. |
CF_DSPTEXT 0x0081 | Text display format associated with a private format. The hMemparameter must be a handle to data that can be displayed in text format in lieu of the privately formatted data. |
CF_ENHMETAFILE 14 | A handle to an enhanced metafile (HENHMETAFILE). |
CF_GDIOBJFIRST 0x0300 | Start of a range of integer values for application-defined GDI object clipboard formats. The end of the range isCF_GDIOBJLAST. Handles associated with clipboard formats in this range are not automatically deleted using the GlobalFree function when the clipboard is emptied. Also, when using values in this range, the hMem parameter is not a handle to a GDI object, but is a handle allocated by the GlobalAlloc function with theGMEM_MOVEABLE flag. |
CF_GDIOBJLAST 0x03FF | See CF_GDIOBJFIRST. |
CF_HDROP 15 | A handle to type HDROP that identifies a list of files. An application can retrieve information about the files by passing the handle to the DragQueryFile function. |
CF_LOCALE 16 | The data is a handle to the locale identifier associated with text in the clipboard. When you close the clipboard, if it contains CF_TEXT data but no CF_LOCALE data, the system automatically sets the CF_LOCALE format to the current input language. You can use the CF_LOCALE format to associate a different locale with the clipboard text. An application that pastes text from the clipboard can retrieve this format to determine which character set was used to generate the text. Note that the clipboard does not support plain text in multiple character sets. To achieve this, use a formatted text data type such as RTF instead. The system uses the code page associated with CF_LOCALE to implicitly convert from CF_TEXT to CF_UNICODETEXT. Therefore, the correct code page table is used for the conversion. |
CF_METAFILEPICT 3 | Handle to a metafile picture format as defined by theMETAFILEPICT structure. When passing a CF_METAFILEPICThandle by means of DDE, the application responsible for deleting hMem should also free the metafile referred to by theCF_METAFILEPICT handle. |
CF_OEMTEXT 7 | Text format containing characters in the OEM character set. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. |
CF_OWNERDISPLAY 0x0080 | Owner-display format. The clipboard owner must display and update the clipboard viewer window, and receive theWM_ASKCBFORMATNAME, WM_HSCROLLCLIPBOARD,WM_PAINTCLIPBOARD, WM_SIZECLIPBOARD, andWM_VSCROLLCLIPBOARD messages. The hMem parameter must beNULL. |
CF_PALETTE 9 | Handle to a color palette. Whenever an application places data in the clipboard that depends on or assumes a color palette, it should place the palette on the clipboard as well. If the clipboard contains data in the CF_PALETTE (logical color palette) format, the application should use theSelectPalette and RealizePalette functions to realize (compare) any other data in the clipboard against that logical palette. When displaying clipboard data, the clipboard always uses as its current palette any object on the clipboard that is in theCF_PALETTE format. |
CF_PENDATA 10 | Data for the pen extensions to the Microsoft Windows for Pen Computing. |
CF_PRIVATEFIRST 0x0200 | Start of a range of integer values for private clipboard formats. The range ends with CF_PRIVATELAST. Handles associated with private clipboard formats are not freed automatically; the clipboard owner must free such handles, typically in response to the WM_DESTROYCLIPBOARD message. |
CF_PRIVATELAST 0x02FF | See CF_PRIVATEFIRST. |
CF_RIFF 11 | Represents audio data more complex than can be represented in a CF_WAVE standard wave format. |
CF_SYLK 4 | Microsoft Symbolic Link (SYLK) format. |
CF_TEXT 1 | Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. Use this format for ANSI text. |
CF_TIFF 6 | Tagged-image file format. |
CF_UNICODETEXT 13 | Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. |
CF_WAVE 12 | Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz PCM. |
标准剪切板数据格式
Windows支持不同的预先定义剪切板格式,这些格式在WINDOWS.INC定义成以CF为前缀的标识符。
首先介绍三种能够储存在剪切板上的文字数据型态,以及一个与剪切板格式相关的数据型态:
- CF_TEXT以NULL结尾的ANSI字符集字符串。它在每行末尾包含一个carriage return和linefeed字符,这是最简单的剪切板数据格式。传送到剪切板的数据存放在整体内存块中,并且是利用内存块句柄进行传送的(我将简短地讨论此项概念)。这个内存块专供剪切板使用,建立它的程序不应该继续使用它。
- CF_OEMTEXT含有文字数据(与CF_TEXT类似)的内存块。但是它使用的是OEM字符集。通常Windows程序不必关心这一点;它只有与在窗口中执行MS-DOS程序一起使用剪切板时才会使用。
- CF_UNICODETEXT含有Unicode文字的内存块。与CF_TEXT类似,它在每一行的末尾包含一个carriage return和linefeed字符,以及一个NULL字符(两个0字节)以表示数据结束。CF_UNICODETEXT只支援Windows NT。
- CF_LOCALE一个国家地区标识符的句柄。表示剪切板文字使用的国别地区设定。
下面是两种附加的剪切板格式,它们在概念上与CF_TEXT格式相似(也就是说,它们都是文字数据),但是它们不需要以NULL结尾,因为格式已经定义了数据的结尾。现在已经很少使用这些格式了:
- CF_SYLK包含Microsoft 「符号连结」数据格式的整体内存块。这种格式用在Microsoft的Multiplan、Chart和Excel程序之间交换数据,它是一种ASCII码格式,每一行都用carriage return和linefeed结尾。
- CF_DIF包含数据交换格式(DIF)之数据的整体内存块。这种格式是由Software Arts公司提出的,用于把数据送到VisiCalc电子表格程序中。这也是一种ASCII码格式,每一行都使用carriage return和linefeed结尾。
下面三种剪切板格式与位图有关。所谓位图就是数据位的矩形数组,其中的数据位与输出设备的图素相对应。后面将详细讨论位图以及这些位图剪切板的格式:
- CF_BITMAP与设备相关的位图格式。位图是通过位图句柄传送给剪切板的。同样,在把这个位图传送给剪切板之后,程序不应该再继续使用这个位图。
- CF_DIB定义一个设备无关位图的内存块。这种内存块是以位图信息结构开始的,后面跟着可用的颜色表和位图数据位。
- CF_PALETTE调色盘句柄。它通常与CF_DIB配合使用,以定义与设备相关的位图所使用的颜色调色盘。
在剪切板中,还有可能以工业标准的TIFF格式储存的位图数据:
- CF_TIFF含有标号图像文件格式(TIFF)数据的整体内存块。这种格式由Microsoft、Aldus公司和Hewlett-Packard公司以及一些硬件厂商推荐使用。这一格式可从Hewlett-Packard的网站上获得。
下面是两个metafile格式。一个metafile就是一个以二进制格式储存的画图命令集:
- CF_METAFILEPICT以旧的metafile格式存放的「图片」。
- CF_ENHMETAFILE增强型metafile(32位Windows支持的)句柄。
最后介绍几个混合型的剪切板格式:
-
- CF_PENDATA与Windows的笔式输入扩充功能联合使用。
- CF_WAVE声音(波形)文件。
- CF_RIFF使用资源交换文件格式(Resource Interchange File Format)的多媒体数据。
- CF_HDROP与拖放服务相关的文件列表。
- CF_PENDATA与Windows的笔式输入扩充功能联合使用。
Synthesized Clipboard Formats
The system implicitly converts data between certain clipboard formats:
if a window requests data in a format that is not on the clipboard, the system converts an available format to the requested format.
The system can convert data as indicated in the following table.
Clipboard Format | Conversion Format |
---|---|
CF_BITMAP | CF_DIB |
CF_BITMAP | CF_DIBV5 |
CF_DIB | CF_BITMAP |
CF_DIB | CF_PALETTE |
CF_DIB | CF_DIBV5 |
CF_DIBV5 | CF_BITMAP |
CF_DIBV5 | CF_DIB |
CF_DIBV5 | CF_PALETTE |
CF_ENHMETAFILE | CF_METAFILEPICT |
CF_METAFILEPICT | CF_ENHMETAFILE |
CF_OEMTEXT | CF_TEXT |
CF_OEMTEXT | CF_UNICODETEXT |
CF_TEXT | CF_OEMTEXT |
CF_TEXT | CF_UNICODETEXT |
CF_UNICODETEXT | CF_OEMTEXT |
CF_UNICODETEXT | CF_TEXT |
If the system provides an automatic type conversion for a particular clipboard format,
there is no advantage to placing the conversion format(s) on the clipboard.
If the system provides an automatic type conversion for a particular clipboard format,
and you call EnumClipboardFormats to enumerate the clipboard data formats,
the system first enumerates the format that is on the clipboard, followed by the formats to which it can be converted.
When copying bitmaps, it is best to place the CF_DIB or CF_DIBV5 format on the clipboard.
This is because the colors in a device-dependent bitmap (CF_BITMAP) are relative to the system palette,
which may change before the bitmap is pasted. If theCF_DIB or CF_DIBV5 format is on the clipboard and
a window requests the CF_BITMAPformat, the system renders the device-independent bitmap (DIB) using the current palette at that time.
If you place the CF_BITMAP format on the clipboard (and not CF_DIB), the system renders the CF_DIB or CF_DIBV5 clipboard format
as soon as the clipboard is closed. This ensures that the correct palette is used to generate the DIB.
If you place theCF_DIBV5 format with the bitmap color space information in the clipboard,
the system will convert the bitmap bits from the bitmap color space to the sRGB color space whenCF_DIB or CF_DIBV5 is requested.
If CF_DIBV5 is requested when there is no color space information in the clipboard,
the system returns sRGB color space information in the BITMAPV5HEADER structure.
Conversions between other clipboard formats occur upon demand.
If the clipboard contains data in the CF_PALETTE format, the application should use the SelectPalette and RealizePalette functions
to realize any other data in the clipboard against that logical palette.
There are two clipboard formats for metafiles: CF_ENHMETAFILE and CF_METAFILEPICT.
Specify CF_ENHMETAFILE for enhanced metafiles and CF_METAFILEPICT for Windows metafiles.
剪切板和Unicode
迄今为止,我只讨论了用剪切板处理ANSI文字(每个字符对应一个字节)。我们用CF_TEXT标识符时就是这种格式。您可能对CF_OEMTEXT和CF_UNICODETEXT还不熟悉吧。
我有一些好消息:在处理您所想要的文字格式时,您只需呼叫SetClipboardData和GetClipboardData,Windows将处理剪切板中所有的文字转换。例如,在Windows NT中,如果一个程序用SetClipboardData来处理CF_TEXT剪切板数据型态,程序也能用CF_OEMTEXT呼叫GetClipboardData。同样地,剪切板也能将CF_OEMTEXT数据转换为CF_TEXT。
在Windows NT中,转换发生在CF_UNICODETEXT、CF_TEXT和CF_OEMTEXT之间。
程序应该使用对程序本身而言最方便的一种文字格式来呼叫SetClipboardData 。同样地,程序应该用程序需要的文字格式来呼叫GetClipboardData。
我们已经知道,本书附上的程序在编写时可以带有或不带UNICODE标识符。
如果您的程序也依此编写,那么在定义了UNICODE标识符之后,程序将执行带有CF_UNICODETEXT参数的SetClipboardData以及GetClipboardData呼叫,而不是CF_TEXT。
Registered Clipboard Formats
Many applications work with data that cannot be translated into a standard clipboard format without loss of information.
These applications can create their own clipboard formats.
A clipboard format that is defined by an application, is called a registered clipboard format.
For example, if a word-processing application copied formatted text to the clipboard using a standard text format,
the formatting information would be lost.
The solution would be to register a new clipboard format, such as Rich Text Format (RTF).
To register a new clipboard format, use the RegisterClipboardFormat function.
This function takes the name of the format and returns and unsigned integer value that represents the registered clipboard format.
To retrieve the name of the registered clipboard format, pass the unsigned integer value to the GetClipboardFormatNamefunction.
If more than one application registers a clipboard format with exactly the same name, the clipboard format is registered only once.
Both calls to theRegisterClipboardFormat function return the same value.
In this way, two different applications can share data by using a registered clipboard format.
Private Clipboard Formats
An application can identify a private clipboard format by defining a value in the range CF_PRIVATEFIRST through CF_PRIVATELAST.
An application can use a private clipboard format for an application-defined data format that does not need to be registered with the system.
Data handles associated with private clipboard formats are not automatically freed by the system.
If your windows use private clipboard formats, you can use theWM_DESTROYCLIPBOARD message to free any related resources
that are no longer needed.
For more information about the WM_DESTROYCLIPBOARD message, see Clipboard Ownership.
An application can place data handles on the clipboard by defining a private format in the range CF_GDIOBJFIRST through CF_GDIOBJLAST.
When using values in this range, the data handle is not a handle to a Windows Graphics Device Interface (GDI) object,
but is a handle allocated by the GlobalAlloc function with the GMEM_MOVEABLE flag.
When the clipboard is emptied the system automatically deletes the object using theGlobalFree function.
到目前为止,我们仅处理了Windows定义的标准剪切板资料格式。但是,您可能想用剪切板来储存「自订数据格式」。许多文书处理程序使用这种技术来储存包含着字体和格式化信息的文字。
初看之下,这个概念似乎是没有意义的。如果剪切板的作用是在应用程序之间传送数据,那么,为什么剪切板中要含有只有一个应用程序才能理解的数据呢?
答案很简单:剪切板允许在同一个程序的内部(或者可能在一个程序中的不同执行实体之间)传送数据。很明显地,这些执行实体能理解它们自己的自订数据格式。
有几种使用自订数据格式的方法。最简单的方法用到一种表面上是标准剪切板格式(文字、位图或metafile)的数据,可是该数据实际上只对您的程序有意义。
这种情况下,在SetClipboardData和GetClipboardData呼叫中可使用下列wFormat值:CF_DSPTEXT、CF_DSPBITMAP、CF_DSPMETAFILEPICT或CF_DSPENHMETAFILE
(字母DSP代表「显示器」)。这些格式允许Windows按文字、位图或metafile来浏览或显示资料。
但是,另一个使用常规的CF_TEXT、CF_BITMAP、CF_DIB、CF_METAFILEPICT或CF_ENHMETAFILE格式呼叫GetClipboardData的程序将不能取得这个数据。
如果用其中一种格式把数据放入剪切板中,则必须使用同样的格式读出数据。
Multiple Clipboard Formats
A window can place more than one clipboard object on the clipboard, each representing the same information in a different clipboard format.
When placing information on the clipboard, the window should provide data in as many formats as possible.
To find out how many formats are currently used on the clipboard, call the CountClipboardFormatsfunction.
Clipboard formats that contain the most information should be placed on the clipboard first, followed by less descriptive formats.
A window pasting information from the clipboard typically retrieves a clipboard object in the first format it recognizes.
Because clipboard formats are enumerated in the order they are placed on the clipboard,
the first recognized format is also the most descriptive.
For example, suppose a user copies styled text from a word-processing document.
The window containing the document might first place data on the clipboard in a registered format, such as RTF.
Subsequently, the window would place data on the clipboard in a less descriptive format, such as text (CF_TEXT).
When the content of the clipboard is pasted into another window, the window retrieves data in the most descriptive format it recognizes.
If the window recognizes RTF, the corresponding data is pasted into the document.
Otherwise, the text data is pasted into the document and the formatting information is lost.
利用多个数据项
当打开剪切板并把数据传送给它时,必须先呼叫EmptyClipboard,通知Windows释放或删除剪切板上的内容。
不能在现有的剪切板内容中附加其它东西。所以,从这种意义上说,剪切板每次只能保留一个数据项。
但是,可以在EmptyClipboard和CloseClipboard呼叫之间多次呼叫SetClipboardData,每次都使用不同的剪切板格式。
例如,如果想在剪切板中储存一个很短的文字字符串,可以把这个文字写入metafile,也可以把这个文字写入位图。
把位图选进内存设备内容中,并把这个字符串写进位图中。利用这种方法可以使字符串不仅能为从剪切板上读取文字的程序所使用,
也可以为从剪切板上读取位图和metafile的程序所使用。当然,这些程序并不能知道metafile或位图实际上包含了一个字符串。
#ifndef NOCLIPBOARD /* * Predefined Clipboard Formats */ #define CF_TEXT 1 #define CF_BITMAP 2 #define CF_METAFILEPICT 3 #define CF_SYLK 4 #define CF_DIF 5 #define CF_TIFF 6 #define CF_OEMTEXT 7 #define CF_DIB 8 #define CF_PALETTE 9 #define CF_PENDATA 10 #define CF_RIFF 11 #define CF_WAVE 12 #define CF_UNICODETEXT 13 #define CF_ENHMETAFILE 14 #if(WINVER >= 0x0400) #define CF_HDROP 15 #define CF_LOCALE 16 #endif /* WINVER >= 0x0400 */ #if(WINVER >= 0x0500) #define CF_DIBV5 17 #endif /* WINVER >= 0x0500 */ #if(WINVER >= 0x0500) #define CF_MAX 18 #elif(WINVER >= 0x0400) #define CF_MAX 17 #else #define CF_MAX 15 #endif #define CF_OWNERDISPLAY 0x0080 #define CF_DSPTEXT 0x0081 #define CF_DSPBITMAP 0x0082 #define CF_DSPMETAFILEPICT 0x0083 #define CF_DSPENHMETAFILE 0x008E /* * "Private" formats don't get GlobalFree()'d */ #define CF_PRIVATEFIRST 0x0200 #define CF_PRIVATELAST 0x02FF /* * "GDIOBJ" formats do get DeleteObject()'d */ #define CF_GDIOBJFIRST 0x0300 #define CF_GDIOBJLAST 0x03FF #endif /* !NOCLIPBOARD */