Disabling main menu items in MFC based apps 
Author Message
 Disabling main menu items in MFC based apps

I am writing an MFC based application, and I want to disable items on my
main window menu depending on the state of the app. MFC provides a nice
mechanism for doing this for menu items that reside one level down in
the menu tree - the ON_UPDATE_COMMAND_UI support. Unfortunately this
mechanism does not work for items on the main menu. 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?
  --Elliot Leonard


Wed, 11 Jul 2001 03:00:00 GMT  
 Disabling main menu items in MFC based apps
Elliot,

The only reason ON_COMMAND_UPDATE_UI handlers don't work for the top-level
menu is that these items are "popup" menus and don't have command ID's. If
you add an immediate command to the top-level menu, then an
ON_UPDATE_COMMAND_UI handler will work just fine. My point is that this is
not a restriction of the top-level menu but of popup menu items in general.
If you cascade a menu off one of your popup menus, you can't control that
item from an ON_UPDATE_COMMAND_UI handler either.

To do what you want to do, try this.

CMenu menu;
menu.LoadMenu(IDR_MAINFRAME); // Substitute your top-level menu resource ID
for the parameter
// In the next line, "index" is the zero-based index to the menu item you
wish to disable
menu.EnableMenuItem(index, MF_GRAYED | MF_BYPOSITION);

You can do this from whatever function changes the application state such
that the menu item is no longer relevant.

--
Dave Smith
MCSE, MCT, MVP

Please post responses, I cannot reply to individual mail

Quote:

>I am writing an MFC based application, and I want to disable items on my
>main window menu depending on the state of the app. MFC provides a nice
>mechanism for doing this for menu items that reside one level down in
>the menu tree - the ON_UPDATE_COMMAND_UI support. Unfortunately this
>mechanism does not work for items on the main menu. 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?
>  --Elliot Leonard



Wed, 11 Jul 2001 03:00:00 GMT  
 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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Thu, 12 Jul 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to disable/enable Popup menu item of the main menu

2. Disabling main menu items

3. Checking menu items in dialog based app.

4. Updating menu items in dialog-based apps: Solution

5. dialog based mfc app that starts without showing the main window

6. PBM: Message box not displayed after main dialog box is closed in MFC dialog-based app

7. Disabling menu option on main menu bar

8. Removing main menu from MFC SDI app

9. Enabling Menu Item in Main Menu?

10. Trying to disable Menu Item and ToolbarCtrl item..

11. Dialog Based Apps - Menu & Windows :: MFC

12. Disable menu item in popup menu

 

 
Powered by phpBB® Forum Software