Various Ways to Access Submenus and Menu Items

Last reviewed: November 2, 1995
Article ID: Q71454
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

In calls to Microsoft Windows functions that create, modify, and destroy menus, an application can access an individual menu item by either its position or its item ID. A pop-up menu must be accessed by its position because it does not have a menu-item ID.

Specifically, when an application calls the EnableMenuItem() function to enable, disable, or dim (gray) an individual menu item, the application can specify either the MF_BYPOSITION or the MF_BYCOMMAND flag in the wEnable parameter. When the application calls EnableMenuItem() to access a pop-up menu, it must specify the MF_BYPOSITION flag.

The information below provides examples of the following:

  • Retrieving a menu handle for a submenu
  • Accessing a submenu
  • Accessing a menu item

MORE INFORMATION

The following resource-file menu template provides the basis for the source code examples in this article. The template describes a top-level menu with two pop-up submenus. One of the submenus contains a third, nested submenu.

   GenericMenu MENU
   BEGIN
      POPUP "&Help"
      BEGIN
         MENUITEM "&About Generic...", IDM_ABOUT
      END

      POPUP "&Test"
      BEGIN
         POPUP "&Nested"
         BEGIN
            MENUITEM "&1 Beep", IDM_1BEEP
            MENUITEM "&2 Beeps", IDM_2BEEPS
         END
      END
   END


Retrieving the Handle to a Submenu

Code such as the following can be used to obtain handles to the menus:

   HMENU hMainMenu, hHelpPopup, hTestPopup, hNestedPopup;

   <other program lines>

   hMainMenu = GetMenu(hWnd);
   hHelpPopup = GetSubMenu(hMainMenu, 0);
   hTestPopup = GetSubMenu(hMainMenu, 1);
   hNestedPopup = GetSubMenu(hTestPopup, 0);

The second parameter of the GetSubMenu() function, nPos, is the position of the desired submenu. Positions are numbered starting at zero for the first menu item.

Disabling a Submenu

The following call disables and dims the nested pop-up menu:

   EnableMenuItem (hTestPopup, 0, MF_BYPOSITION | MF_GRAYED);

The following call disables and dims the Test pop-up menu:

   EnableMenuItem (hMainMenu, 1, MF_BYPOSITION | MF_GRAYED);

The second parameter of the EnableMenuItem() function, wIDEnabledItem, is the position of the submenu. As above, positions are numbered starting at zero. Note that the call must specify the MF_BYPOSITION flag because a pop- up menu does not have a menu-item ID.

Disabling a Menu Item

The 1 Beep menu item can be disabled and dimmed by using any one of the following calls:

   EnableMenuItem(hMainMenu, IDM_1BEEP, MF_BYCOMMAND | MF_GRAYED);
   EnableMenuItem(hTestPopup, IDM_1BEEP, MF_BYCOMMAND | MF_GRAYED);
   EnableMenuItem(hNestedPopup, IDM_1BEEP, MF_BYCOMMAND | MF_GRAYED);
   EnableMenuItem(hNestedPopup, 0, MF_BYPOSITION | MF_GRAYED);

A menu item can be specified by either by its menu-item ID value (using the MF_BYCOMMAND flag) or by its position (using the MF_BYPOSITION) flag. If the application specifies the menu-item ID value, Windows must walk the menu structure and search for a menu item with the correct ID. This implies the each menu-item ID value must be unique for a given menu.

Other Windows Menu Functions

Although the EnableMenuItem() function is used in the example above, the same general approach is used for all Windows menu functions; access pop-up menus by position, and access menu items by position or menu- item ID.


Additional reference words: 3.00 3.10 3.50 4.00 95 dimmed unavailable
KBCategory: kbui
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: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.