Basic technology of windows-menu programming

Source: Internet
Author: User

Basic technology of windows-menu programming
First, Introduction
In many of the frameworks are unavoidable to operate the menu, whether it is qt,android or iOS, the basic application framework will involve the menu API calls, but the change is not the case, the basic concept of the menu we know, we can better understand and call, The basic operation of the menu in MFC is relatively simple, which is a bit complex, even if you implement the menu command response function, which involves the use of MFC message mapping mechanism.

two, categories of MFC Menus
1. Pop-up menu
The default top-level menu is a pop-up menu, you cannot respond to menu commands, and the menu is applied as pop-up, you need to tick the pop-up property, such as.

2. Menu bar, submenu, menu item the distinction of three

I believe everyone should be able to see it, this is the blogger in the book summed up, absolutely correct.

third, the routing of MFC menu commands
The basic line is: View->doc->frame->app
Specific process Analysis: When we click on a menu item, the first thing to accept is the frame class. The frame class gives the received message to the child window view class, which is processed first. The view class determines whether or not it responds to this message according to the message mapping mechanism, and the response is processed and the message is routed to completion. Do not respond to the Doc class, the Doc class also determine whether they respond to the message, the response is processed, the message is returned to the view class, the view class will then give this message to the frame class, frame to determine whether they respond to this command message, the response is processed, No response will be handed over to the App class processing.

Four, Windows message classification
1. Standard messages
All but wm_command are standard messages that start with wm_.
2. Command messages
A message from a menu, accelerator key, or toolbar button, which is presented in wm_command form, distinguishing different commands by ID.
3. Notification message
The message that the control produces, such as the click of a button, is intended to notify its parent of the occurrence of the event, and such messages are rendered in wm_command form.
from CWnd-derived classes, three messages can be received.
A class derived from CCmdTarget cannot receive a standard message.

Five, basic menu Operation
1. Note : The Separator bar is also a menu item and must be noted.

2. Mark Menu
Image:

To create the code:

  #if 0  // flag menu Getmenu () ->
     getsubmenu (0 ) ->  checkmenuitem (0 , Mf_byposition |        mf_checked);    /* the value of the first parameter of many menu functions is determined by the second argument */     #endif          #if 0  // Set the tag menu by ID getmenu () /span>->  getsubmenu (0 ) ->  checkmenuitem (id_file_new, Mf_bycommand |    mf_checked);  #endif   

3. Default menu items (menu items shown in bold)
Note: A submenu branch can have only one default menu item
Image:

To create the code:

    #if 0        //Set the default menu item by index GetMenu ()GetSubMenu (0)Setdefaultitem (1, TRUE);#endif    #if 0        //Set the default menu item by ID GetMenu ()GetSubMenu (0)Setdefaultitem (Id_file_open, FALSE);#endif    #if 0        //Set a menu item after the separator line to print as the default menu items, and the split line is also counted as an item, so the printed index is4(Split line) +1=5GetMenu ()GetSubMenu (0)Setdefaultitem (5, TRUE);#endif

4. Graphical markup Menu
Introduction: A menu item has a corresponding graphic tag in front of it.
Picture:

Make sure that you set the bitmap to a size similar to the size specified in the system, and you can use the GetSystemMetrics function

 #if 1  // sets the graphical marker menu CString str; /* // x = 15  y = 15  the bitmap size for the default Graphics markup menu is 15  * 15  str. Format (_t ( "x =%d, y =%d" ), GetSystemMetrics (Sm_cxmenucheck), GetSystemMetrics (sm_    Cymenucheck));    MessageBox (str); */// set graphical marker menu Getmenu () 
      ->  getsubmenu (0 ) ->  setmenuitembitmaps (0 , Mf_byposition, &m_bitmap, &M_BITMAP);  #endif   

5. Disable the menu item
Note: The distinction between disable and grayed out , in general, disabling a menu item will make it grayed out.
In addition, in order to disable the menu item, you must cancel the MFC default command update mechanism, set in the CMainFrame constructor: m_bautomenuenable = FALSE; You can
create the code:

  #if 0  // disable menu items by index getmenu ()   getsubmenu (0 ) ->  enablemenuitem (1 , Mf_byposition | mf_disabled |    mf_grayed);     #endif          #if 0  // disable menu items by ID getmenu () 
      ->  getsubmenu (0 ) ->  enablemenuitem (Id_file_open, Mf_bycommand | mf_disabled | mf_grayed);  #endif   

6. Remove the menu bar
In the framework class of MFC, you can set the object to not display the menu bar.
Set the code

#if 0    //移除菜单    SetMenu(nullptr);#endif

7. Create a menu bar object
MFC also gives us a way to create a menu bar object
To create the code:

#if0    //创建菜单    menu;    menu.LoadMenu(IDR_MAINFRAME);    SetMenu(&menu);    //分离菜单句柄与菜单对象,防止局部变量出错    menu.Detach();#endif

8. Shortcut menu (context menu)
Introduction: Is the menu item that we right-click Popup in the program again

Create a 3-step process
First step : Add menu resources, set ID

Step two : Add Wm_rbuttondown messages for classes that need to respond to the right-click message

Step three : Set the response function for menu items

9. Dynamic menu Operation
All of the above is a static operation of the menu, which describes the dynamic operation of the menu.
9**.1 Menu Bar Trailing submenu * *

#if0    //添加弹出式子菜单    menu;    menu.CreateMenu();    GetMenu()->AppendMenu(MF_POPUP, (UINT_PTR)(menu.m_hMenu), _T("Test2"));    menu

9.2 Insert submenu in menu bar

 #if0    //插入子菜单    menu;    //先要创建menu对象    menu.CreateMenu();     //插入子菜单    GetMenu()->InsertMenu(2, MF_POPUP | MF_BYPOSITION, (UINT_PTR)(menu.m_hMenu), _T("Test3"));    //因为是局部变量的menu,所以需要分离    menu.Detach();#endif

9.3 Delete the corresponding menu item according to the object calling the function

  #if 0  // delete 1  submenu getmenu () ->  deletemenu (1 , mf_byposition);  #endif   #if 0  // Delete the first submenu with an index of 2  menu item Getmenu  () ->  getsubmenu 0 ) ->  DeleteMenu ( 2 , mf_byposition);  #endif   

Six, MFC menu command update mechanism
Each submenu below the menu item needs to set its own state, this need to set the response of the update mechanism, MFC provides us with this update mechanism, the use of MFC programming, the state of the menu item maintenance depends on the UPDATE_COMMAND_UI message, but can only be applied to menu items.
Here is an example that allows us to control whether the cut below is available for editing

1. Add Cut update processing command

2. Define Update response function

This allows us to set the state of the menu item for clipping.

* * Here are some of the principles of the MFC command update mechanism:
When the menu is to be displayed, Nishinaga emits a wm_initmenupopup message, which is then taken over by the base class of the program window, creates a CCmdUI object and associates it with the first menu item in the Program submenu, invokes the object's Layer member function doupdate (), This function emits a UPDATE_COMMAND_UI message with an object pointer to CCmdUI. At this point, the system will determine whether the class has ON_UPDATE_COMMAND_UI macro to capture the menu item message, find the call response function processing, when the first menu item is updated, the same CCmdUI object is set to be associated with the second menu item, sequentially, Until the processing of all menu items is complete. This is the command update mechanism for MFC menu items. **

Seven, summary
This time the main introduction of the menu-related programming technology, are basic API usage and concepts, lay the foundation is very important, the following is the use of information.
Self-summarized mind mapping

NET disk information: Https://yunpan.cn/cPbJkXRRvnkNe access password 0a8f

Basic technology of windows-menu programming

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.