
Disabling main menu items in MFC based apps
Quote:
>No doubt, Microsoft
>would say that disabling main menu items is bad form, but that is
>absurd. Does anyone know a simple work around for this problem?
The tricky thing about this problem is not disabling the menubar item
- after all, Enable MenuItem will do that for you - but deciding
whether or not the item need to be disabled in the first place.
Prior to the advent of the ON_COMMAND_UI_UPDATE mechanism, I used to
do this with a recursive menu-walking algorithm that I was
inordinately proud of, but nowadays I've resorted to another
mechanism.
I create a subfunction which is called by the update handler, e.g.
void CMainFrame::OnUpdateConfigurePrintOut (CCmdUI* pCmdUI)
{
pCmdUI -> Enable (Conf_PrintOut_Available ());
Quote:
}
BOOL CMainFrame::Conf_PrintOut_Available ()
{
if (<some test or other>)
return TRUE;
else
return FALSE ;
Quote:
}
The point being that the subfunction can then be called from
elsewhere. For instance, the update handler for the first item in a
popup is called twice - once for the item and once for the popup
itself. So the handler for the first item in a popup can be :
afx_msg void CMainFrame::OnUpdateConfigureCharsDwellinglocn(CCmdUI*
pCmdUI)
{
// This is the first item in a popup, and gets called once for
// the popup, then again for the item. In the first case, the
// m_pSubMenu member variable is none-null. In the first
// case, we must decide whether or not to enable the popup
// as a whole.
if (pCmdUI->m_pSubMenu != NULL)
{
// enable popup if ANY subitem is available.
BOOL bPupEnable = Conf_Chars_Dl_Available () ||
Conf_Chars_R_Available () ||
Conf_Chars_Sc_Available () ||
Conf_Chars_Sel_Available ();
pCmdUI->m_pMenu->EnableMenuItem (pCmdUI->m_nIndex,
MF_BYPOSITION |
(bPupEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
}
else
{
pCmdUI -> Enable (Conf_Chars_Dl_Available ());
}
Quote:
}
But you can see that we can similarly decide whether to enable the top
level menubar item or not, by simply calling all the enable
subfunctions below it :
BOOL CMainFrame::MenuBar_Configure_Available ()
{
return (Conf_RO_Available () ||
Conf_RI_Available () ||
Conf_EqM_Available () ||
Conf_EqT_Available () ||
Conf_Chars_Dl_Available () ||
Conf_Chars_R_Available () ||
Conf_Chars_Sc_Available () ||
Conf_Chars_Se_Available () ||
Conf_PrintOut_Available () ||
Conf_Ti_Available () );
Quote:
}
I simply call these master enable/disable routines anytime something
happens which might result in one of them being disabled.
Bob Moore [MVP]
http://www.mooremvp.freeserve.co.uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Due to an unreasonable amount of queries, I no
longer answer unsolicited email questions. Sorry,
no exceptions.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~