Using Multiple Data Items

When you open the clipboard to put data into it, you must call EmptyClipboard to signal Windows to free or delete the contents of the clipboard. You can't add something to the existing contents of the clipboard. So in this sense, the clipboard holds only one item at a time.

However, between the EmptyClipboard and the CloseClipboard calls, you can call SetClipboardData several times, each time using a different clipboard format. For instance, if you want to store a short string of text in the clipboard, you can create a metafile device context and write that text to the metafile. You can also create a bitmap large enough to hold the character string, select the bitmap into a memory device context, and write the string to the bitmap. In this way, you make that character string available not only to programs that can read text from the clipboard but also to programs that read bitmaps and metafiles from the clipboard. Moreover, if you select a different font into the metafile device context or memory device context before writing the text, programs that read bitmaps or metafiles will use the string with this different font. (Of course, these programs won't be able to recognize the metafile or bitmap as actually containing a character string.)

If you want to write several handles to the clipboard, you call SetClipboardData for each of them:

OpenClipboard (hwnd) ;

EmptyClipboard () ;

SetClipboardData (CF_TEXT, hGMemText) ;

SetClipboardData (CF_BITMAP, hBitmap) ;

SetClipboardData (CF_METAFILEPICT, hGMemMFP) ;

CloseClipboard () ;

While these three formats of data are in the clipboard, an IsClipboardFormatAvailable call with the CF_TEXT, CF_BITMAP, or CF_METAFILEPICT argument will return TRUE. A program can get access to these handles by calling:

hGMemText = GetClipboardData (CF_TEXT) ;

or:

hBitmap = GetClipboardData (CF_BITMAP) ;

or:

hGMemMFP = GetClipboardData (CF_METAFILEPICT) ;

The next time a program calls EmptyClipboard, Windows will free or delete all three of the handles retained by the clipboard as well as the metafile that is part of the METAFILEPICT structure.

A program can determine all the formats stored by the clipboard by first opening the clipboard and then calling EnumClipboardFormats. Start off by setting a variable wFormat to 0:

wFormat = 0 ;

OpenClipboard (hwnd) ;

Now make successive EnumClipboardFormats calls starting with the 0 value. The function will return a positive wFormat value for each format currently in the clipboard. When the function returns 0, you're done:

while (wFormat = EnumClipboardFormats (wFormat))

{

[logic for each wFormat value]

}

CloseClipboard () ;

You can obtain the number of different formats currently in the clipboard by calling:

nCount = CountClipboardFormats () ;