Sun Xin video tutorial Lesson 6-menu

Source: Internet
Author: User

The sixth lesson of Sun Xin's video tutorial is about how to create a menu bar from the static and dynamic aspects and modify the menu bar! Menu working principle and writing application, menu command message transmission sequence and processing process in several classes of the MFC framework program. Mark menu, implementation principle of default menu, implementation of graphic menu and Analysis of Common Mistakes, application of getsystemmetrics, shortcut menu implementation method and effective range of its command response function (it is closely related to the parent window specified when the menu is popped up, and the underlying child window has the highest processing opportunity ). Writing dynamic menus, how to generate new menu items during the program running, and how to manually arrange processing functions for these new menu commands, how to intercept the processing of menu commands in the top-level window to further understand the application of the cstring class.

1. Message classification and response:

Standard MessageAll messages starting with WM _ Except wm_command. Classes derived from cwnd can receive such messages. Command MessageMessages 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. Classes derived from csf-target can receive such messages. Announcement messageMessages generated by the control, 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 such messages. The following describes the message response principle of our instance: when a message is transferred to oncommand, The cmainframe class receives the message and transfers it to the cview class. If the cview class has a response function, it returns the response. If not, it is handed to the cdoc class, if the cdoc class has a response function, the response is returned. If not, the response is returned to the cview class, and then the cview class is handed over to the cmainframe class. If the cmainframe class has a response function, the response is returned, if not, the cmainframe class will be handed over to the CAPP class for response! Therefore, the order of the response function is: cview class-> cmainframe class-> cdoc class-> CAPP class! (PS: the pop-up window does not support message response. Therefore, we need to deselect the topic "popup" in the menu attribute !)
2. A. How to Create a tag menu: to easily create a tag menu, we need to have a familiar understanding of the menu! You can see the following figure:
The following functions are required to create a tag menu: getmenu () returns a pointer to the menu bar, getsubmenu () returns a pointer to the sub menu, and checkmenuitem ()! Create statement: Add the following code to the oncreate () function of cmainframe (): getmenu ()-> getsubmenu (0)-> checkmenuitem (0, mf_byposition | mf_checked ); // access getmenu ()-> getsubmenu (0)-> checkmenuitem (idm_test, mf_bycommand | mf_checked) by index number; // access by ID! B. create default menu items: (Note that only one default menu item can be created in a sub-menu, and the index number must be obtained with a separator.) getmenu () -> getsubmenu (0)-> setdefaultitem (1, true); getmenu ()-> getsubmenu (0)-> setdefaultitem (id_file_open); C. create a graphic tag menu item: (there is a difference between vs2008 and vc6.0: In vs2008, you do not need to obtain the system measurement function, but you need to use this function getsystemmetrics () in vc6.0 ()) m_bitmap1.loadbitmapw (idb_bitmap1 );
Getmenu ()-> getsubmenu (0)-> setmenuitembitmaps (0, mf_byposition, & m_bitmap1, & m_bitmap1); D. make menu items unavailable: this function can be easily implemented by using options in properties, but you need to note that you need to set the m_bautomenuenable variable to false in the constructor of the cmainframe function, the following code takes effect: getmenu ()-> getsubmenu (0)-> enablemenuitem (1, mf_byposition | mf_disabled | mf_grayed); but after you set m_bautomenuenable to false, there will be another problem: the menu items that could not be used have been changed to available, which can use the method in F! E. How to cancel the entire menu: You can directly use the setmenu (null) function. The following code calls the menu again, but pay attention to it! Cmenu menu; // when this variable is a local variable, you need to add the menu. the detach () function disconnects the variable from the C ++ object, so that no structure will be generated!
Menu. loadmenuw (idr_mainframe );
Setmenu (& menu );
Menu. Detach (); F. Command update mechanism: maintenance of 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 in it. You can manually or use classwizard to add the on_update_command_ui macro to the Message ing to capture the cn_update_command_ui message. In the background, the operating system sends the wm_initmenupopup message, which is then taken over by the basic class of MFC, such as cframewnd. It creates a ccmdui object and is associated with the first menu item. 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. The same ccmdui object is set to be associated with the second menu item, so that the order is performed until all menu items are completed. The update command UI handler is only applicable to projects in pop-up menu items, and cannot be applied to permanently displayed top-level menu items. In this way, you can add a message processing program to an unusable menu item! Example: void cmainframe: onupdateeditcut (ccmdui * pcmdui)
{
// Todo: add the command here to update the user interface handler code
Pcmdui-> enable ();
} The cut menu item can be used! G. in the View class, right-click the menu to bring up the menu function. In vs2008, you cannot manually add a menu function as in vc6.0. Therefore, you need to manually write code to implement the function, add a message response function onrbuttondown () to the View class and add a menu resource. The Code is as follows: cmenu menu;
Menu. loadmenuw (idr_menu1 );
Cmenu * popup = menu. getsubmenu (0 );
Clienttoscreen (& Point); // The coordinates are converted from the customer's area to the screen coordinates!
Popup-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, point. x, point. y, this); // create the code above the pop-up menu to enable right-click the menu in the View class to bring up the menu!
3. The following describes how to add a menu dynamically:
The main functions required for dynamic menu addition are: createpopmenu (), appenmenu (), insertmenu (), and deletemenu (). Here we need to differentiate the main functions of each function. Do not make a mistake! The first function mainly creates an empty menu. The second function mainly adds a menu after the menu bar, but its functions are different in different places, in the following code, we can see that the third function is to add the required menus between the two menus! The fourth function is to delete a menu! Their implementations are of different use in different places. For specific implementations, see the following code: Implement cmenu menu in the oncreate () function of the cmainframe () class;
Menu. createpopupmenu ();
// Getmenu ()-> appendmenuw (mf_popup, (uint) menu. m_hmenu, text ("winsun"); // Add a menu after the existing menu
Getmenu ()-> insertmenuw (2, mf_byposition | mf_popup, (uint) menu. m_hmenu, text ("winsun"); // insert a menu between existing menus!
Menu. appendmenuw (mf_string, 111, text ("hello"); // Add a menu item
Menu. appendmenuw (mf_string, 112, text ("winsun "));
Menu. appendmenuws (mf_string, 113, text ("mybloe "));
Menu. Detach (); getmenu ()-> getsubmenu (0)-> appendmenuw (mf_string, 114, text ("welcome"); // Add a menu item to the sub-menu
Getmenu ()-> getsubmenu (0)-> insertmenuw (2, mf_byposition | mf_string, 115, text (""); // insert a menu item in the submenu!
Getmenu ()-> deletemenu (1, mf_byposition); // delete a menu
Getmenu ()-> getsubmenu (0)-> deletemenu (1, mf_byposition); // delete a menu item!
4. Compile the Telephone Program:
The main function of this phone book is: when you write a person's name and phone number in a View window, add the name to the menu bar by yourself, then, when I want to click the sub-menu item on the menu bar, the name and phone number of this person are displayed on our view class. The implementation code is as follows:

Void cid063view: onchar (uint nchar, uint nrepcnt, uint nflags) // Add a message response function to the View class!
{
// Todo: add the message processing program code and/or call the default value here
Cclientdc DC (this );
If (0x0d = nchar)
{
If (0 = + m_nindex)
{
M_menu.createpopupmenu ();
Getparent ()-> getmenu ()-> appendmenuw (mf_popup, (uint) m_menu.m_hmenu, text ("Phonebook"); // obtain the pointer of the parent class first, then, the pointer of the parent class points to the subclass.
Getparent ()-> drawmenubar (); // re-paint the menu bar! In this way, the menu created in the View class can be displayed!
}
M_menu.appendmenuw (mf_string, idm_phone1 + m_nindex, m_strline.left (m_strline.find ('')));
M_strarray.add (m_strline); // Add the input string to the Collection class and save it!
M_strline.empty (); // clear the string
Invalidate (); // invalidate the client region of the entire window!
}
Else
{
M_strline + = (tchar) nchar;
DC. textoutw (0, 0, m_strline );
}

Cview: onchar (nchar, nrepcnt, nflags );
}

Void cid063view: onphone1 () // This is the response function for dynamically adding menu items!
{
// Todo: add the command handler code here
Cclientdc DC (this );
DC. textoutw (0, 0, m_strarray.getat (1 ));
}

Void cid063view: onphone2 ()
{
// Todo: add the command handler code here
Cclientdc DC (this );
DC. textoutw (0, 0, m_strarray.getat (2 ));
}

Void cid063view: onphone3 ()
{
// Todo: add the command handler code here
Cclientdc DC (this );
DC. textoutw (0, 0, m_strarray.getat (3 ));
}

Void cid063view: onphone4 ()
{
// Todo: add the command handler code here
Cclientdc DC (this );
DC. textoutw (0, 0, m_strarray.getat (4 ));
}

The following code truncates the message response path of the cview class and is responded by the cmainframe class. Note that the header file contains:

Bool cmainframe: oncommand (wparam, lparam)
{
// Todo: Add dedicated code and/or call the base class here
Int menustmid = loword (wparam); // you only need to obtain the low byte order of one byte.
Cid063view * pview = (cid063view *) getactiveview (); // get the pointer of the current view class
If (menustmid> = idm_phone1 & menustmid <idm_phone1 + pview-> m_strarray.getsize ())
{
Cclientdc DC (pview );
DC. textoutw (0, 0, pview-> m_strarray.getat (menucmdid-idm_phone1 ));
Return true;
}

Return cframewnd: oncommand (wparam, lparam );
}

(Add a virtual function in vs2008: Right-click the class you need and select "Override" in the attribute !)

 

The above content is a review of Sun Xin's main knowledge points in Lesson 6!

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.