SAMPLE: Owner-Draw Menu Item Width Includes a Check Mark Width

Last reviewed: February 15, 1996
Article ID: Q71061
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

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().

The Microsoft Software Library contains an example, MENUBMP, of owner-draw menus with bitmaps where each item is only as large as the bitmap.

Download MENUBMP.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download MENUBMP.EXE (size: 25065 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get MENUBMP.EXE (size: 25065 bytes) 
    

MORE INFORMATION

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 reference words: 3.00 3.10 softlib MENUBMP.EXE
KBCategory: kbprg kbfile
KBSubcategory: UsrMen


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.