Home > MFC > Working with menus and toolbars in MFC!

Working with menus and toolbars in MFC!

MFC requires us to treat menu’s in a special way, we just can’t enable a menu item by calling EnableMenuItem. So many have asked this query, particularly newbies :).

Well so tell us how we can do this?

MFC for toolbars and menuitems works differently! You need to add a handler for ON_UPDATE_COMMAND_UI.

For eg:

// Declare OnDisableFileNew likewise!
afx_msg void OnDisableFileNew( CCmdUI* pCmdUI );

// and define OnDisableFileNew likewise!
void CMainFrame::OnDisableFileNew( CCmdUI* pCmdUI )
     // Add code for disabling menu item
     pCmdUI->Enable( FALSE );

You may say this is tiresome, but this is how MFC document view architecture works. This applies only to MDI and SDI apps, with dialogs you can have your own way!

It’s not just Enable or Disable that works this way even checking(checkbox, radiobox) of toolbar and menu items works this way. Look up CCmdUI for more functions!

There is also another macro called ON_UPDATE_COMMAND_UI_RANGE which deals with a range of controls provided command id’s are consecutive. For e.g. you have a range of color menu items starting from Red to Blue or whatever. So in this case you can use ON_UPDATE_COMMAND_UI_RANGE macro but make sure that command id’s are consecutive.

This macro takes three arguments, first and last command id along with the handler function name.

For eg:

ON_UPDATE_COMMAND_UI_RANGE( FirstId, LastId, OnUpdateCmdUIRangeHandler )
  1. Popescu Radu Alexandru (aka u0m3)
    May 11, 2009 at 6:28 pm

    Is this supposed to work with (Visual Studio 2008 SP1)/(WinSDK v6.0A/v6.1)? I can’t get it to work under any circumstances.

    • May 11, 2009 at 6:32 pm


      This is quite a famous way of working with menus and toolbars in MFC and should work in VS2008 SP1 as well, but watch out I couldn’t test this out with MFC feature pack.


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: