In addition to text, Windows lets you retrieve a bitmap from the clipboard and display it in your client area. To retrieve and display a bitmap, use the same technique as for pasting text, but make a few changes to accommodate bitmaps.
First, you must modify the WM_INITMENU case in the window function so that it recognizes the CF_BITMAP format. After you change it, the WM_INITMENU case should look like this:
case WM_INITMENU:
if (wParam == GetMenu(hWnd)) {
if (OpenClipboard(hWnd)) {
if (IsClipboardFormatAvailable(CF_BITMAP))
EnableMenuItem(wParam, IDM_PASTE, MF_ENABLED);
else
EnableMenuItem(wParam, IDM_PASTE, MF_GRAYED);
CloseClipboard();
return (TRUE);
}
else /* Clipboard is not available */
return (FALSE);
}
Although retrieving a bitmap from the clipboard is as easy as retrieving text, displaying a bitmap requires more work than does displaying text. In general, you need to do the following:
1.Retrieve the bitmap data handle from the clipboard. Bitmap data handles from the clipboard are GDI bitmap handles (created by using functions such as CreateBitmap).
2.Create a compatible display context and select the data handle into it.
3.Use the BitBlt function to copy the bitmap to the client area.
4.Release the bitmap handle from the current selection.
After you have changed the IDM_PASTE case, it should look like this:
case IDM_PASTE:
if (OpenClipboard(hWnd)) {
/* get text from the clipboard */
if (!(hClipData = GetClipboardData(CF_BITMAP))) {
CloseClipboard();
break;
}
if (!(lpClipData = GlobalLock(hClipData))) {
OutOfMemory();
CloseClipboard();
break;
}
hDC = GetDC(hWnd);
1 hMemoryDC = CreateCompatibleDC(hDC);
if (hMemoryDC != NULL) {
2 GetObject(hClipData, sizeof(BITMAP),
&PasteBitmap);
3 hOldBitmap = SelectObject(hMemoryDC,
hClipData);
if (!hOldBitmap) {
BitBlt(hDC, 10, 10,
PasteBitmap.bmWidth,
PasteBitmap.bmHeight,
hMemoryDC, 0, 0, SRCCOPY);
SelectObject(hMemoryDC, hOldBitmap);
}
4 DeleteDC(hMemoryDC);
}
ReleaseDC(hWnd, hDC);
GlobalUnlock(hClipData);
CloseClipboard();
GlobalUnlock(hText);
}
break;
In this example:
1 | The CreateCompatibleDC function returns a handle to a display context, in memory, that is compatible with your computer's display. This means any bitmaps that you select for this display context can be copied directly to the client area. If CreateCompatibleDC fails (returns NULL), the bitmap cannot be displayed. |
2 | The GetObject function retrieves the width and height of the bitmap, as well as a description of the bitmap format. It copies this information into the PasteBitmap structure, whose size is specified by the sizeof function. In this example, only the width and height are used and then only in the BitBlt function. |
3 | The SelectObject function selects the bitmap into the compatible display context. If it fails (returns NULL), the bitmap cannot be displayed. SelectObject may fail if the bitmap has a different format than that of the compatible display context. This can happen, for example, if the bitmap was created for a display on some other computer. |
4 | The DeleteDC function removes the compatible display context. Before a display context can be deleted, its original bitmap must be restored by using the SelectObject function. |