Friday, June 11, 2010

How to disable menu items in VC++

How to disable a menu item in VC++?

06/11/2010

.h:

afx_msg void OnUpdateXXX(CCmdUI* pCmdUI);

.cpp:

ON_UPDATE_COMMAND_UI(ID_NEW_XX, OnUpdateXXX)

void CXXXXXX::OnUpdateXXX(CCmdUI *pCmdUI)
{

pCmdUI->Enable(FALSE);

//pCmdUI->Enable(TRUE);

}

NOTE: the following doesnot work properly:

CWnd* pParent = GetParent(); // This is a point to the window
CMenu* pMenu = pParent->GetMenu();
pMenu->EnableMenuItem(ID_NEW_WO , MF_GRAYED );

Reason:
The effects of EnableMenuItem are preempted by the actions of the menu
update handlers. For example, if you use EnableMenuItem() to disable a
menu item, the default menu update handler will reenable it if an
ON_COMMAND macro is defined for that item. This, off course, negates
the effect of your attempt to disable it. To eanable or disable a menu
item, you should provide your own menu update handler and then use the
Enable() member function of CCmdUI.

No comments: