Menu command update mechanism and menu command mechanism

Source: Internet
Author: User

Menu command update mechanism and menu command mechanism

When using MFC programming, maintenance of menu item status depends on CN_UPDATE_COMMAND_UI messages. You can manually or use the Class Wizard to add the ON_UPDATE_COMMAND_UI macro to the Message ing to capture the CN_UPDATE_COMMAND_UI message. UPDATE_COMMAND_UI the response of a message can only be used for menu items. It cannot be used for permanently displayed top-level menus (that is, pop-up menus ).

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)    ON_WM_CREATE()    ON_UPDATE_COMMAND_UI(ID_FILE_NEW, &CMainFrame::OnUpdateFileNew)END_MESSAGE_MAP()

After the program framework captures the CN_UPDATE_COMMAND_UI message, it is finally processed by the response function of the message.

Void CMainFrame: OnUpdateFileNew (CCmdUI * pCmdUI) {// TODO: add the command here to update the user interface handler code pCmdUI-> Enable (FALSE ); // disable pCmdUI-> SetCheck (); // select the tag pCmdUI-> SetText (L "New"); // modify the text of the "New" menu item to "New "}

The CCmdUi class can be used to determine whether a menu item is available, whether it is marked, and whether the text of the menu item can be changed.

In the preceding example, an UPDATE_COMMAND_UI message response function is added to the new menu item under the file sub-Menu Using the Class Wizard. In the background, MFC sends the WM_INITMENUPOPUP message when the menu is to be displayed, which is then taken over by the base class of the program window, such as CFrameWnd. It creates a CCmdUI object and is associated with the first menu item of the program. It calls a member function DoUpdate of the object. This function sends a CN_UPDATE_COMMAND_UI message with a pointer to the CCmdUI object. At this time, the system will determine whether there is an ON_UPDATE_COMMAND_UI macro to capture this menu item message. If such a macro is found, the corresponding message response function is called for processing. In this function, the passed CCmdUI object can be used to call the corresponding function, make the menu item available or disabled. After the first menu item is updated, the same CCmdUI object is set to be associated with the second menu item, in this order, until all menu items are processed. This is the command update mechanism used by MFC.

 

Related Article

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.