Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 284
Peter Nimmo Avatar asked Dec 02 '09 19:12

Peter Nimmo


2 Answers

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();
}
like image 76
BostonLogan Avatar answered Oct 02 '22 12:10

BostonLogan


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.

like image 35
djeidot Avatar answered Oct 02 '22 13:10

djeidot