views:

500

answers:

2

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  A: 

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();
}
BostonLogan
Thanks for your help, that seems to have done the trick.
Stone Free
I think yours is the only solution because ON_UPDATE_COMMAND_UI handlers only get activated when someone clicks on a menu. In the case of a normal menu any items that possibly need to be disabled are only made to appear when the user clicks on the top level menu and this of course kicks off OnInitMenuPopup and in turn the ON_UPDATE_COMMAND_UI handlers
Stone Free
According to MS If a menu bar is changed after Windows has created the window, call this function to draw the changed menu bar.
BostonLogan
A: 

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.

djeidot
I followed KB242577 so that ON_UPDATE_COMMAND_UI would be possible but as I said before "This works fine for sub-menu items, but not for the top level menu."It doesn't update the visual display until the item in question is clicked
Stone Free