Michen
Michen

Reputation: 61

MFC Menu Item remains grayed

I have a CDialogEx Class called Properties in which I handle the ON_COMMAND message. Now, ON_COMMAND should get called when I click the menu item ID_EDIT_PROPERTIES (as a submenu from main menu). The event handler wizard wrote that code for me, but when I start the Application the menu item remains grayed out. I've tried to manually activate it by calling EnableMenuItem when ON_UPDATE_COMMAND_UI happens, but to no avail.

Any help would be greatly appreciated.

Upvotes: 3

Views: 3323

Answers (3)

Andrew Truckle
Andrew Truckle

Reputation: 19087

I admit that I don't know if it is different for CDialogEx, But for CDialog I found that the UPDATE_COMMAND_UI didn't ever work unless I handled the WM_KICKIDLE event.

In your OnKickIdle event handler make a call to:

CWnd::UpdateDialogControls

There is a short tutorial on it here.

Forgive me if CDialogEx supercedes this information and I will remove the answer.

Upvotes: 0

Constantine Georgiou
Constantine Georgiou

Reputation: 3401

You just need to understand how menu items enabling/disabling is handled:

  • If there is neither ON_COMMAND nor ON_UPDATE_COMMAND_UI handler the item is disabled.
  • If there exists no ON_UPDATE_COMMAND_UI handler but there is an ON_COMMAND one in the currently active document or view (or even the "mainframe"), the item is enabled.
  • If there exists a ON_UPDATE_COMMAND_UI handler, en-/disabling the item is determined by the handler (pCmdUI->Enable(bEnableState)).

Also keep in mind that:

  • You may not call EnableMenuItem() yourself, instead call pCmdUI->Enable(bEnableState) in an ON_UPDATE_COMMAND_UI handler. This affects not only the menu item, but any other "command"-type item (with the same ID), eg main menu, context menu, toolbar or rebar button.
  • Where to put the handler, is a matter of application design and depends on the data you are processing or representing. It can be put in the mainframe class (if it depends on some "global" data or setting), in the document class (if it depends on or changes some data or setting in the document - possibly affecting all views), or in the view class(-es) (depending on or affecting the current view only).

In your case, if I understand correctly, the item is disabled because the handler is in the CDialogEx-derived class, but no instance of this class has been created yet, ie there exists no ON_COMMAND handler for your ID_EDIT_PROPERTIES command.

Upvotes: 2

Santosh
Santosh

Reputation: 1815

Per m_bAutoMenuEnable, When this data member is enabled (which is the default), menu items that do not have ON_UPDATE_COMMAND_UI or ON_COMMAND handlers will be automatically disabled when the user pulls down a menu.

Upvotes: 2

Related Questions