MFC Study Notes 4 ---- menu programming and Windows message Classification

Source: Internet
Author: User

First, in MFC, menus with the popup attribute cannot be used for command response.

In MFC:

When you click a menu item, the cmainframe framework class receives the menu item message first. The cmainframe framework class delivers the menu item message to its subwindow View class, the View class processes the menu item first. If the View class detects that the menu item message is not responded to, the view class submits the menu item message to the doc class of the document class for processing; if the doc class detects that no response is made to the menu item message in the doc class, the doc class returns the menu item message to the View class, and the View class returns it to the cmainframe class for processing. If the cmainframe class shows that the cmainframe class does not respond to the message, it is finally handed over to the app class for processing.

Therefore, the message transmission sequence is: View class -- Doc class -- cmainframe class -- app class. Of course, once a menu message responds in one of the classes, it no longer finds the Response Function in other classes.

Windows message Classification

(1) Standard Messages: All messages starting with WM _ Except wm_command. All messages derived from the cwnd class can receive these messages.

(2) Command Message: Messages from menus, acceleration keys, or toolbar buttons. These messages are all presented as wm_command. In MFC, different command messages are distinguished by the menu item ID. In SDK, messages are identified by the wparam parameter.

(3) Announcement: Messages generated by controls, such as Button clicking and selection of the list box, are generated to notify the event to its parent window (usually a dialog box. Such messages are also presented in the form of wm_command. ------ Classes derived from csf-target can receive [Command Message] and [advertise message].

Several functions need to be understood during the menu operation:

 1   Cmenu  *  Getmenu ();  //  Cwnd: getmenu to get the window menu bar object pointer.  
2 Cmenu * Getsubmenu (); // Cmenu: getsubmenu get pointer to the pop-up menu object
3 Uint checkmenuitem (); // Cmenu: checkmenuitem add check marks (select tag) or remove check marks, which is used for menu items
4 Bool setdefaultitem (); // Cmenu: setdefaultitem
5 Bool setmenuitembitmaps (); // Cmenu: setmenuitembitmaps sets the bitmap Title menu.
6 Uint enablemenuitem (); // Cmenu: enablemenuitem makes the menu item valid, invalid, or grayed out.
7 Bool setmenu (cmenu * Pmenu ); // Cwnd: setmenu set a new menu or remove menu in the current window
8 Hmenu detach (); // Cmenu: Detach detaches a Windows menu from a cmenu object and returns the handle.

A simple process for creating a menu item:

1 Cmenu menu;//Defined as a local object
2 Menu. loadmenu (idr_mainframe );
3 Setmenu (&Menu );
4 Menu. Detach (); // because of a local object

The menu object serves as a local object. Detach () is used to detach the window menu handle from the menu object, so that the window menu resources are not destroyed when the menu object is destructed.

Command update mechanism:

Maintenance of the menu item status depends on the cn_update_command_ui message. When the cn_update_command_ui message is captured, MFC creates a ccmdui object.
Wm_initmenupopup messages are sent in the background operating system. Then, the basic class of MFC, such as cframewnd, takes over and creates a ccmdui object and associates it with the first menu item to call the object member function doupdate () (Note: this function does not find the description in msdn) sends a cn_update_command_ui message with a pointer to the ccmdui object. After that, the same ccmdui object is set to be associated with the second menu item, so that the order is executed until all menu items are completed.
Update command UI ProcessingProgramOnly applies to items on pop-up menu items, and cannot apply to permanently displayed top-level menu items.

Call the trackpopupmenu function to create a contextmenu (right-click menu ):

 1   Void  Cmenu2view: onrbuttondown (uint nflags, cpoint point)
2 {
3 Cmenu menu;
4 Menu. loadmenu (idr_menu1 );
5 Cmenu * Ppopup = Menu. getsubmenu ( 0 );
6 Clienttoscreen ( & Point ); // Coordinates coordinates a coordinate point or a rectangular area into screen coordinates.
7 Ppopup -> Trackpopupmenu (tpm_leftalign, point. X, point. Y, getparent ()); // The pop-up menu is displayed at the specified position in the specified way.
8 Cview: onrbuttondown (nflags, point );
9 }

1) use resource manager to add a menu resource
2) Right-click the message response function, load the menu resource, obtain the sub-menu pointer to be displayed, and call the trackpopupmenu function to complete the task (note: the coordinates transmitted by the mouse response function are client zone coordinates, while the trackpopupmenu function uses screen coordinates. Before calling trackpopupmenu, call clienttoscreen to convert the client Region coordinates to the screen coordinates)

3) the message in the pop-up menu still follows the response sequence of view-doc-mainframe-app during routing.

Dynamic menu programming:
Some related important functions:

 1   Cmenu: appendmenu  //  Appends a new item to the end of a menu.  
2 Cmenu: createpopupmenu // Creates an empty pop-up menu and attaches it to a cmenu object.
3 Cmenu: insertmenu
4 // Inserts a new menu item at the position specified by nposition and moves other items down the menu.
5 Cmenu: getsubmenu // Retrieves a pointer to a pop-up menu.
6 Cwnd: getmenu // Retrieves a pointer to the menu for this window.
7 Cmenu: deletemenu // Deletes an item from the menu.
1 Cview*Getactiveview ()Const;//Obtain the current window port pointer (in the single document Framework)

Keep a record of what you have learned !!! If I am a snail bot, I can only climb up slowly!

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.