13.1.3 Pasting Bitmaps from the Clipboard

In addition to retrieving text, your Windows application can retrieve a bitmap from the clipboard and display it in the client area. To make the application retrieve and display a bitmap, you use the same technique as for pasting text, but you make a few changes to accommodate bitmaps.

First, modify the WM_INITMENU case in the window procedure 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 == (WPARAM) GetMenu(hwnd)) {
        if (OpenClipboard(hwnd)) {
            if (IsClipboardFormatAvailable(CF_BITMAP))
                EnableMenuItem((HMENU) wParam, IDM_PASTE, MF_ENABLED);

            else
                EnableMenuItem((HMENU) wParam, IDM_PASTE, MF_GRAYED);

            CloseClipboard();
            return TRUE;
        }


        else    /*  Clipboard not available             */
            return FALSE;
    }

    return TRUE;

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 must do the following:

1.Retrieve the bitmap data handle from the clipboard. Bitmap data handles from the clipboard are graphics device interface (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.

For more information about displaying a bitmap, see Chapter 11, “Bitmaps.”