ID Number: Q71454
3.00
WINDOWS
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.
For a list of all Windows menu functions, see page 1-56 of the
"Microsoft Windows Software Development Kit Reference--Volume 1." For
more information on working with menus in a Windows application, see
Chapter 7 of the "Microsoft Windows Software Development Kit Guide to
Programming" for version 3.0.
Additional reference words: 3.00 dimmed unavailable