How to dynamically add menus/menu items, sub-menus, right-click menus, and menu item sub-menus

Source: Internet
Author: User

How to dynamically add menus/menu items, sub-menus, right-click menus, and menu item sub-menus

Menu-related operations mainly use the CMenu class. Of course, the corresponding API functions are also available. The CMenu class is only the encapsulation of the function of the Operation menu in the API by MFC. However, if you can use a class, try to use the class. The class is well organized and the code looks comfortable. For SDK programming, use the API.

CMenu menuMain, menu1; // define CMenu object 1. Create a menu. There are two methods:

1. Use the LoadMenu function to load Resources

MenuMain. LoadMenu (IDR_MAINFRAME); // load from the resource. Here the main menu resource of SDI is used.

2. Create with the CreateMenu Function

Menu1.CreateMenu (); // create a menu. No menu item exists. 2. Add a menu item. AppendMenu () can be added at the end of the menu, and InsertMenu () can be added at the specified position.

// ID_TEST1 is in Resource. define in h and give an integer at will. Do not repeat the existing menu1.AppendMenu (MF_STRING, ID_TEST1, "Test1"); // menu1.AppendMenu (MF_STRING, ID_TEST2, "Test2"); // menu1.InsertMenu (1, MF_BYPOSITION | MF_STRING, (UINT) ID_TEST1, "ID_TEST1") of the second menu item "); // Add a new menu item before the second menu item
3. Add sub-menu

AppendMenu () and InsertMenu () functions are also used. However, pay attention to parameter settings.

Menu1.AppendMenu (MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT) menuMain. GetSubMenu (0)-> m_hMenu, "sub menu ");

// The second parameter is the menu handle HMENU. 4. Delete the menu.

Use the DeleteMenu () and RemoveMenu () functions to delete menu/menu items at the specified position.

Difference between the two: If the menu item is a pop-up menu, the difference between DeleteMenu and RemoveMenu is very important. DeleteMenu clears the pop-up menu, but RemoveMenu does not clear it. One is a thorough deletion, and the other is a removal.

MSDN:

1. The DeleteMenu function destroys the handle to the menu or submenu and frees the memory used by the menu or submenu.

It invalidates the handle of the menu or sub-menu (destroys ).
2. RemoveMenu does not destroy the menu or its handle, allowing the menu to be reused.

It can be reused without deleting the menu from the memory.

5. Add a shortcut menu
CMenu menu1; dimension (); // dynamically create a pop-up menu object menu1.AppendMenu (MF_STRING, ID_TEST1, "menu item 1"); menu1.AppendMenu (MF_STRING, ID_TEST2, "menu item 2 "); menu1.InsertMenu (2, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT) menuMain. m_hMenu, "sub-menu"); // Add sub-menu CPoint pt; GetCursorPos (& pt); menu1.TrackPopupMenu (TPM_RIGHTBUTTON, pt. x, pt. y, this); menu1.DestroyMenu ();
6. Respond to menu events

1. If a menu is added to a resource, use Class Wizard to add a menu RESPONSE event.

2. If the menu is created by code, you need to manually Implement Message ing of the menu. This example is in the CmainFrame class. Of course, it can also be in the View class and Doc class. It can also be based on the dialog box.

1) In the. h file

// Generated message map functionsprotected: // {AFX_MSG (CMainFrame) afx_msg int OnCreate (maid); afx_msg void OnChangmenuitem (); // Add the menu command here to process the function declaration //} AFX_MSGDECLARE_MESSAGE_MAP ()
2) In the. cpp file,

Except (CMainFrame, CFrameWnd) // {AFX_MSG_MAP (CMainFrame) ON_WM_CREATE () ON_COMMAND (IDM_CHANGMENUITEM, OnChangmenuitem) // Add it here. Note that ';' //} else () void CMainFrame: OnChangmenuitem () {// write the code to be processed here ......}
Other methods:

If the menu ID value is continuous, it is best to use ON_COMMAND_RANGE to map the message processing function. You can process all messages within a range in a function.

When you press a menu item, a WM_COMMAND message is sent, and the idnumber of the menu item is included in the low position of the wParam parameter.

BOOL CYourView: OnCommand (WPARAM wParam, LPARAM lParam) {// TODO: Add your specialized code here and/or call the base class UINT m_nItemID = LOWORD (wParam ); if (m_nItemID = ID_YOURITEM) // ID_YOURITEM indicates the ID number specified when you add a menu item {// The response code} return CScrollView: OnCommand (wParam, lParam );}
You can use the TrackPopupMenu return value to process the context menu. Set TPM _ returncmd in the uFlags parameter. In this way, the returned value is the ID of the menu item you selected and can be processed according to the ID.
TrackPopupMenu(TPM_ RETURNCMD ,pt.x,pt.y,this);

MSDN:If you specify TPM_RETURNCMD in the uFlags parameter, the return value is the menu-item identifier of the item that the user selected.VII. Others

DrawMenuBar (); // when you change a menu, you need to re-paint the menu to display the changes.

GetSystemMenu (); // get the window control window

GetMenu () // obtain the menu used by the current program

GetSubMenu () // obtain the submenu

The Detach () member function of the CMenu class should be used to separate the menu handle from the Cmenu object to avoid program errors after the object fails.

For example:

CMenu menu; menu. createPopupMenu (); // dynamically create the menu object menu. appendMenu (0, ID_TEST1, "Test1"); menu. appendMenu (0, ID_TEST2, "Test2"); CMenu * menuMain = GetMenu (); // obtain the main menu of the program. menuMain-> AppendMenu (MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT) menu. m_hMenu, "sub menu 1"); menu. detach (); // use menu directly. m_hMenu encountered an error during running. The menu object destroyed DrawMenuBar () at the end of this event ();

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.