SAMPLE: MenuBmp.exe Contains Owner-Draw Menus with Bitmaps

ID: Q71061

The information in this article applies to:

SUMMARY

When an application creates an owner-draw menu item with the MF_OWNERDRAW style, the application receives a WM_MEASUREITEM message for that item. The application is required to fill the MEASUREITEMSTRUCT pointed to by the lParam of this message.

The itemWidth field of the MEASUREITEMSTRUCT is normally filled with the actual width of the item. However, when Windows displays that menu item, the width is increased by the width of a check mark; that is, Windows automatically expands the item to leave space for a check mark.

To make the menu item only as wide as the actual item, fill the itemWidth field with the width of the item, minus the width of a check mark, as returned by GetMenuCheckMarkDimensions().

MenuBmp.exe is a sample that contains an example of owner-draw menus with bitmaps where each item is only as large as the bitmap.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

 ~ MenuBmp.exe (size: 25065 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

The following code fragment demonstrates processing a WM_MEASUREITEM message that results in a menu item only as wide as the actual item. In this case, the menu is being made with bitmaps. The handle to the bitmap is stored as part of the item data.

   // Local variables.
   LPMEASUREITEMSTRUCT     lpItem;
   HBITMAP                 hBitmap;
   BITMAP                  bm;
   WORD                    cxCheck;

   ...

   case WM_MEASUREITEM:
       // lParam is a pointer to the structure.
       lpItem = (LPMEASUREITEMSTRUCT)lParam;

       // A bitmap handle was stored in the item data.
       hBitmap = LOWORD(lpItem->itemData);

       /*
        * The width of a check mark is automatically added to
        * menu items so we need to subtract it to make the
        * menu the minimum size.
        */ 
       cxCheck = LOWORD(GetMenuCheckMarkDimensions());

       // Get the bitmap dimensions
       GetObject(hBitmap, sizeof(BITMAP), (LPVOID)&bm);

       // Set the width to the width of the bitmap - cxCheck
       lpItem->itemWidth = bm.bmWidth - cxCheck;

       // Add one to the bitmap height for some spacing.
       lpItem->itemHeight = bm.bmHeight + 1;

       break;

   ...

Additional query words: Keywords : kbcode kbfile kbsample kbMenu kbGrpUser kbWinOS310 kbWinOS300

Last Reviewed: December 24, 1998