Microsoft KB Archive/120598

{|
 * width="100%"|

PRB: Accelerator Key Not Disabled with ON_UPDATE_COMMAND_UI

 * }

Q120598

-

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), used with:
 * Microsoft Visual C++, versions 1.5, 1.51, 1.52

-

SYMPTOMS
If a popup menu contains a submenu and a menu item on that submenu uses an accelerator key, you can't enable or disable the accelerator key by calling CCmdUI::Enable in the ON_UPDATE_COMMAND_UI for that menu item.

Example
For example, if an application has the following menu structure for the main application window, pressing CTRL+M sends the command for MenuItem.

  File  Other Popup MenuItem Ctrl+M Item Ctrl+T The MenuItem command can be disabled and enabled by using an ON_UPDATE_COMMAND_UI handler such as this:

  void CMyView::OnUpdateMenuItem(CCmdUI* pCmdUI) {        pCmdUI->Enable(m_bEnabled); } However, this doesn't enable or disable the accelerator key (CTRL+M). To see whether the accelerator was disabled or enabled correctly, select the MenuItem menu item manually by choosing "Other," then "Popup," and then "MenuItem." This calls the ON_UPDATE_COMMAND_UI function to disable (grey out) the MenuItem command.

CAUSE
The ON_UPDATE_COMMAND_UI handler is called for menu items from the OnInitMenuPopup handler of the menu's frame window. Windows only sends the WM_INITMENUPOPUP message to top-level popups, not submenus, when an accelerator key is used. This explains why the accelerator key for Item in the menu above will be disabled as expected but not the menu item in the submenu.

RESOLUTION
To resolve the problem:


 * Use a flag in your menu item handler to see whether this menu item should be enabled or not. If it shouldn't be enabled, return immediately from the function without processing the command. You will still want to use an ON_UPDATE_COMMAND_UI handler for disabling (greying out) the menu item.
 * Disable the menu item explicitly by using the EnableMenuItem function. Continue to use the ON_UPDATE_COMMAND_UI handler as well.

Additional query words: 1.50 2.50 2.51 2.52 pop-up pop up

Keywords :

Issue type : kbprb

Technology : kbAudDeveloper kbMFC