7.5.6 Using Bitmaps as Menu Items

You can also use bitmaps as menu items. There are two ways to do this:

When you insert or append a new item, specify that you want to use a bitmap instead of text for that item.

Use the ModifyMenu function to change an existing item so that it appears as a bitmap instead of text.

You cannot specify a bitmap as a menu item in the .RC file.

The following example loads a bitmap named Apples and then uses the ModifyMenu function to replace the text of the Apples menu item with this bitmap image of an apple.

HMENU hMenu;
HBITMAP hBitmap;
    .
    .
    .

hBitmap = LoadBitmap(hinst, "Apples");

hMenu = GetMenu(hWnd);
ModifyMenu(hMenu,
    IDM_APPLES,               /* item to replace             */
    MF_BYCOMMAND | MF_BITMAP,
    IDM_APPLES,               /* menu identifier of new item */
    (LPSTR) MAKELONG(hBitmap, 0))

In this example, the LoadBitmap function first loads the bitmap from the file and returns a handle of the bitmap, saved in the hBitmap variable.

The GetMenu function then retrieves the handle of the current window's menu and places it in the variable hMenu. This variable is passed as the first parameter of the ModifyMenu function, which specifies the menu to change. The second parameter of the ModifyMenu function—in this case, IDM_APPLES—specifies the item to be modified.

The third parameter specifies how to make the changes. MF_BYCOMMAND indicates to Windows that you are specifying the item to be changed by its menu identifier rather than by its position. MF_BITMAP indicates that the new item will be a bitmap rather than text.

The fourth parameter, set to IDM_APPLES, specifies the new menu identifier for the item being modified. In this example, the menu identifier does not change.

The new bitmap handle must be passed as the low-order word of the fifth parameter of ModifyMenu. The MAKELONG macro combines the 16-bit handle with a 16-bit constant to make the 32-bit argument. Casting the parameter to an LPSTR data type prevents the compiler from issuing a warning, since the compiler “expects” this parameter to be a string.