共用方式為


CMenu::EnableMenuItem

Enables, disables, or dims a menu item.

UINT EnableMenuItem(
   UINT nIDEnableItem,
   UINT nEnable 
);

Parameters

  • nIDEnableItem
    Specifies the menu item to be enabled, as determined by nEnable. This parameter can specify pop-up menu items as well as standard menu items.

  • nEnable
    Specifies the action to take. It can be a combination of MF_DISABLED, MF_ENABLED, or MF_GRAYED, with MF_BYCOMMAND or MF_BYPOSITION. These values can be combined by using the bitwise OR operator. These values have the following meanings:

    • MF_BYCOMMAND   Specifies that the parameter gives the command ID of the existing menu item. This is the default.

    • MF_BYPOSITION   Specifies that the parameter gives the position of the existing menu item. The first item is at position 0.

    • MF_DISABLED   Disables the menu item so that it cannot be selected but does not dim it.

    • MF_ENABLED   Enables the menu item so that it can be selected and restores it from its dimmed state.

    • MF_GRAYED   Disables the menu item so that it cannot be selected and dims it.

Return Value

Previous state (MF_DISABLED, MF_ENABLED, or MF_GRAYED) or –1 if not valid.

Remarks

The CreateMenu, InsertMenu, ModifyMenu, and LoadMenuIndirect member functions can also set the state (enabled, disabled, or dimmed) of a menu item.

Using the MF_BYPOSITION value requires an application to use the correct CMenu. If the CMenu of the menu bar is used, a top-level menu item (an item in the menu bar) is affected. To set the state of an item in a pop-up or nested pop-up menu by position, an application must specify the CMenu of the pop-up menu.

When an application specifies the MF_BYCOMMAND flag, Windows checks all pop-up menu items that are subordinate to the CMenu; therefore, unless duplicate menu items are present, using the CMenu of the menu bar is sufficient.

Example

// The code fragment below shows how to disable (and gray out) the 
// File\New menu item.
// NOTE: m_bAutoMenuEnable is set to FALSE in the constructor of 
// CMainFrame so no ON_UPDATE_COMMAND_UI or ON_COMMAND handlers are 
// needed, and CMenu::EnableMenuItem() will work as expected.

CMenu* mmenu = GetMenu();
CMenu* submenu = mmenu->GetSubMenu(0);
submenu->EnableMenuItem(ID_FILE_NEW, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

Requirements

Header: afxwin.h

See Also

Reference

CMenu Class

Hierarchy Chart

CMenu::GetMenuState

EnableMenuItem

Other Resources

CMenu Members