Various Ways to Access Submenus and Menu ItemsLast reviewed: November 2, 1995Article ID: Q71454 |
The information in this article applies to:
SUMMARYIn 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:
MORE INFORMATIONThe 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 SubmenuCode 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 SubmenuThe 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 ItemThe 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 FunctionsAlthough 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |