Why can't I turn off / turn off the menu? (MFC)

I am trying to disable / populate menu items using the CMenu::EnableMenuItem() method.

I have a CMenu* pMenu variable that refers to the top menu of the dialog. I can get the submenu using pMenu->GetSubMenu(int) and using submenu->GetMenuStringA() , check the names of the submenus / menu items that I will return. However, I am having problems with the EnableMenuItem() method. Let me say that this is the File menu. It has New and Open popup menus and Import , Close and Close All menu items. New and Open have submenu items. (for example, New->Document ) Using submenu->EnableMenuItem([position of submenu/menuitem], MF_BYPOSITION | MF_GRAYED); , I can disable New or Open , however the function does not work for Import , Close and Close All , as well as all menu items with New and Open .

Note. When I say that EnableMenuItem() fails, I do not mean that it returns -1. It returns the previous status, but the menu does not become disabled or inaccessible.

In the MSDN documentation for EnableMenuItem() : http://msdn.microsoft.com/en-us/library/h62wh3y1.aspx, he claims that this will work for pop-ups as well as for standard menu items. However, this seems to only work for pop-ups.

+7
c ++ mfc
source share
2 answers

A, claimed by ScottMcP-MVP MFC, has a menu configuration in the ON_UPDATE_COMMAND_UI handler: when a user pulls a menu, each menu item must know whether it should be displayed as enabled or disabled. The purpose of the menu command is to provide this information by implementing the ON_UPDATE_COMMAND_UI handler. For each of the command UI objects in your application, use the Properties window to create a record and message map for each handler.

When the menu is pulled out, the environment searches for and calls each ON_UPDATE_COMMAND_UI handler, each handler calls CCmdUI member functions such as Enable and Check, and the structure then displays each menu item accordingly.

This means that you must store the expected state for the menu item in your classes, which you can check or uncheck. You will need to place one ON_UPDATE_COMMAND_UI macro in the menu item next to the ON_COMMAND macro, and this item will refer to a function that receives the CCmdUi object, which you can change to suit your needs. But since you use MFC, you usually do not do this manually, but simply use the properties of windows containing menus.

+2
source share

MFC has another on / off menu item scheme, and this scheme overrides what you are doing. To work in the MFC chart, you add ON_UPDATE_CMD_UI message handlers, as described here:

http://msdn.microsoft.com/en-us/library/6kc4d8fh.aspx

+7
source share

All Articles