13.3.3 Adding a WM_INITMENU Case

To prepare the Edit menu for pasting, you must add a WM_INITMENU case to your window procedure. In general, the Paste command should not be available in this menu unless there is selected text in the clipboard to paste. Add the following statements to the window procedure:

case WM_INITMENU:

    if (wParam == (WPARAM) GetMenu(hwnd)) {
        if (OpenClipboard(hwnd)) {
            if (IsClipboardFormatAvailable(CF_TEXT)
                    || IsClipboardFormatAvailable(CF_OEMTEXT))
                EnableMenuItem((HMENU) wParam, IDM_PASTE, MF_ENABLED);

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

            CloseClipboard();
            return TRUE;
        }

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

    return TRUE;

These statements process the WM_INITMENU message only if the specified menu is found on the menu bar. The IsClipboardFormatAvailable function determines whether text data is present in the clipboard. If data is present, the EnableMenuItem function enables the Paste command. Otherwise, the Paste command is disabled.