怎么让弹出菜单的某一个菜单项变灰(不可用)?

YufengShi 2005-11-01 02:28:34
rt
...全文
249 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangding 2005-11-01
  • 打赏
  • 举报
回复
用EnableMenuItem()并带MF_GRAYED参数
菜牛 2005-11-01
  • 打赏
  • 举报
回复
在菜单弹出之前自己设置菜单项的状态。如上所说。
powerbamboo 2005-11-01
  • 打赏
  • 举报
回复
CMenu::EnableMenuItem
UINT EnableMenuItem( UINT nIDEnableItem, UINT nEnable );

Return Value

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

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.
Remarks

Enables, disables, or dims a menu item. 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);

jmcooler 2005-11-01
  • 打赏
  • 举报
回复
变灰不是用enable,而是用EnableMenuItem()并带MF_GRAYED参数调用。
kenskens 2005-11-01
  • 打赏
  • 举报
回复
或者用EnableMenuItem()
kenskens 2005-11-01
  • 打赏
  • 举报
回复
处理菜单的UPDATE_COMMAND_UI
void CMainFrame::OnUpdateXXXX(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if( xxx )
{
pCmdUI->Enable();
}
else
{
pCmdUI->Enable(0); // 变灰(不可用)
}
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧