Microsoft KB Archive/71454

-

{| 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
 * width="100%"|

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 &quot;&Help&quot; BEGIN MENUITEM &quot;&About Generic...&quot;, IDM_ABOUT END

POPUP &quot;&Test&quot; BEGIN POPUP &quot;&Nested&quot; BEGIN MENUITEM &quot;&1 Beep&quot;, IDM_1BEEP MENUITEM &quot;&2 Beeps&quot;, 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;

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.
 * }

-

Last reviewed: November 2, 1995

© 1998 Microsoft Corporation. All rights reserved. Terms of Use.