How can I disable and gray the top level menu item using MFC

大兔子大兔子 提交于 2019-12-10 17:08:32

问题


I have a dialog application in which I want to have clickable menu items at the top of the dialog. These items do not show a drop down menu but actually run the associated commands.

I did this by setting Popup=False in the dialogs properties and assigning a message-id but my problem is not having the ability to disable the item properly when it makes no sense for the item to be clickable (depending on internal state stored in the dialog)

I have already found out how to disable any popup-parent menu items from http://www.microsoft.com/msj/0299/c/c0299.aspx, but this isn't exactly what I want

I have also found out how to add menu command routing to dialogs from the msdn knowledgebase article KB242577.

This works fine for sub-menu items, but not for the top level menu.

I am currently using the following function to do the disabling

void CYourDlg::EnableMenuItem(UINT nCommand, BOOL bEnable)
{
   CMenu* pMenu = GetMenu();
   pMenu->EnableMenuItem(nCommand, bEnable ? 0 : MF_DISABLED | MF_GRAYED);
}

This half works, if you alt-tab away from the app it does show as disabled, otherwise it doesn't.

Is there a way to invalidate the area programmatically?

I think an non-client area message may be involved.


回答1:


I have not tried but in regular window (not dialog) CWnd::DrawMenuBar should do what you want. It might work with dialog based applications as well.

void CYourDlg::EnableMenuItem(UINT nCommand, BOOL bEnable)
{
   CMenu* pMenu = GetMenu();
   pMenu->EnableMenuItem(nCommand, bEnable ? 0 : MF_DISABLED | MF_GRAYED);
   DrawMenuBar();
}



回答2:


I think you should add an ON_UPDATE handler for your menu ID. This would ensure that the menu is enabled/disabled when you want to.



来源:https://stackoverflow.com/questions/1835209/how-can-i-disable-and-gray-the-top-level-menu-item-using-mfc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!