PRB: Accelerator Key Not Disabled with ON_UPDATE_COMMAND_UI

Last reviewed: July 22, 1997
Article ID: Q120598
1.50 1.51 1.52 WINDOWS kbprg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++ for Windows, version 1.5, 1.51, and 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 reference words: 1.50 2.50 2.51 2.52 pop-up pop up
KBCategory: kbprg kbcode
KBSubcategory: MfcUI
Keywords : kb16bitonly
Technology : kbMfc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.