How to enable/disable menu items

Source: Internet
Author: User
Tags knowledge base

This problem has occurred several times in the previous knowledge base, and many people have asked about the Enable or disable menu in an MFC application, and calling Cmenu::enablemenuitem in the main menu doesn't work ... How do I disable menu items?

According to past experience, to solve this problem, there seems to be an API function like EnableMenuItem, which functions as a enable or disable menu item. Windows does have such functions-but not in MFC applications. In fact, the Enable or disable menu item in MFC is implemented by using On_ update_command_ui. Let me first explain why MFC implementations differ from the standard C and Windows APIs, as well as the benefits of MFC's implementation methods.

In general, the state of the user interface refers to the button, status, menu items, and so on anything that reflects the state of the program. For example, if the program is in read-only mode, the edit command should be disable, and there may be a small indicator in the program that prompts the user for this state. Another example is if the Clipboard has no content (a state), then the paste (Paste) command in the menu should be disable. So the usual user interface (UI) refers to the state of the program's performance, and the change in the state of the program should be reflected in the menu of the program.

How do I get a user interface that reflects the state of the program at any time? There are two ways of my own:

The first is neurotic, which means that whenever the program state changes, do not forget to update the user interface at the same time, if the user calls read-only mode command, the command to disable by the edit control. Similarly, if the user calls cut or copy, the processor immediately paste the command. Any changes to the state of the program anywhere in the program must be updated with the corresponding UI.

The second approach is to relax, that is, not to try to maintain all state information, but to update the user interface only as needed. For menus, you do not have to keep the status of the menu updated, only when the display is updated.

This method is easier and simpler. More importantly, it separates the data from the user interface. Each object only stores its own state-for example, the document knows when it is in read-only mode. The UI can explain the various states that appear, and you don't want low-level objects to invoke similar Enablemenu UI functions! MFC provides a UI update mechanism to implement the latter method, and the detailed method description is discussed in another article because it is too much, and the basic idea is this: when a user invokes a menu, Windows sends a WM_INITMENUPOPUP message. MFC creates a temporary CCmdUI object that processes the message, initializes each menu item consecutively, and passes it to the object in the application. MFC updates the user interface for this call to the ON_UPDATE_COMMAND_UI Message Processor:

ON_UPDATE_COMMAND_UI (Id_foo, Onupdatefoo)

MFC calls the Onupdatefoo function whenever the user enters a menu item that contains Foo. You don't have to worry about having to call:: EnableMenuItem (the first method) everywhere; All you have to do is determine the menu state from the program state. The typical processing methods are as follows:

void CMainFrame::OnUpdateFoo(CCmdUI* pCmdUI)
{
  
    pCmdUI->Enable(pObj->GetFoo());
}

Getfoo () is a hypothetical function that obtains the Foo state of an object-for example, M_pdocument->getreadonly (). There may be 20 functions to modify the Foo state (naturally by method Setfoo), but there is only one place to update the menu. Of course, there may be more complex situations, such as:

Pcmdui->enable (M_bfoo &&

(Getstatusx (...) | | Getstatusy (...));

In the case of the Paste menu, you have to check whether the Clipboard has pasted content, which may be text or graphics, where the key is to determine the state of the menu when needed. The menu update code is placed separately in a function-away from potential objects-instead of sprinkler-enablemenuitem calls across all objects.

MFC uses CCmdUI and ON_UPDATE_COMMAND_UI to adjust the status of buttons, status bar panes, and menu items, and you can expand other UI projects yourself. For example, when the user clicks the drop-down arrow, you can adjust the contents of the combo box or list box according to the state of the program. Ccmdui::enable is a virtual function, in the operation of a menu item, it becomes:: EnableMenuItem.

In the previous example, the UI processing we discussed was implemented in CMainFrame, but you can also place this processing in frames, views, documents, applications (derived from CWinApp) or any other class, and commands are sent through ccmdtarget::oncmdmsg. If MFC cannot find the ON_UPDATE_COMMAND_UI for a particular menu, it automatically makes the enable or disable using the following rules:

If the command has a processor (ON_COMMAND), MFC enable menu items; Otherwise, MFC disable menu items. You can set cframewnd::m_bautomenuenable = False to overload this behavior, so that all menu items will be enable-regardless of whether or not there is a processor.

Therefore, in the MFC application, do not use EnableMenuItem to enable or disable the menu, but to use the ON_UPDATE_COMMAND_UI processor to implement the menu enable or disable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.