MFC menu programming-summary.

Source: Internet
Author: User

Menu structure

    1. A menu bar can have several sub-menus, and a sub-menu can have multiple sub-menus.
      For sub-menus in the menu bar, the index starts from 0 from left to right.
      For the menu items of a specific sub-menu, an index starting from 0 is created from top to bottom.
      You can access sub-menus and menu items through their indexes or identifiers.
    2. Right-click the menu displayed in the client area of the window. If the menu belongs to the View class window, the menu item can only respond to the view and Doc class message clicks.
    3. If the menu ownership Framework Window is displayed, the message routing on the menu follows the response sequence of view-doc-mainframe-app.

 

Menu-related important functions

  1. Cmenu * getmenu (); // obtain the menu pointer.
  2. Cmenu * getsubmenu (); // obtain the sub-menu pointer, that is, the pop-up menu pointer.
  3. Uint checkmenuitem (); // add or remove the √ mark
    A. if the first parameter is an ID, the second parameter must be a combination of mf_bycommand | mf_checked.
    B. If the first parameter is an index number, the second parameter must be a combination of mf_byposition | mf_checked.
  4. Bool setdefaultitem (); // sets the default menu, that is, the menu item is displayed in bold.
    A. if the first parameter is an index number, the second parameter must be true.
    B. If the first parameter is an ID, the second parameter must be false.
    * Note: A sub-menu can have at most one default menu item.
  5. Bool setmenuitembitmaps (); // sets the bitmap tag. The tag size is 13*13 pixels.
    A. if the first parameter is an ID, the second parameter must be mf_bycommand.
    B. If the first parameter is an index number, the second parameter must be mf_byposition.
    The third parameter is the bitmap when no selection is made, and the fourth parameter is the bitmap when marking
  6. Uint enablemenuitem (); // make the menu item valid, invalid, or grayed out
    A. if the first parameter is an ID, the second parameter must be mf_bycommand and a valid, invalid, or grayed out combination.
    B. If the first parameter is an index number, the second parameter must be mf_byposition and valid, invalid, or grayed out combination.
    * Note: To make this function take effect, you must add m_bautomenuenable = false to the mainfrm constructor,
    At this time, the other dimmed menu items will be restored to the gray state, and there will be side effects.
  7. Bool setmenu (cmenu * pmenu); // set a new menu or remove menu in the current window.
    If the parameter is 0, the menu is removed.
    Create a menu {cmenu menu; menu. loadmenu (idr_menu1) by yourself );
    Setmenu (& menu); menu. Detach ();}
  8. Hmenu detach (); // If the cmenu object is set as a local object, use detach () to detach the window menu handle from the menu object, therefore, when the menu object is destructed, The Window Menu resources are not destroyed accordingly.

 

Implementation of menu-related operations

A. Add a check mark:
Method 1: getmenu ()-> getsubmenu (0)-> checkmenuitem (0, mf_byposition | mf_checked); // Index
Method 2: getmenu ()-> getsubmenu (0)-> checkmenuitem (id_file_new, mf_bycommand | mf_checked); // use the ID

 

B. Set the default menu items:

Each sub-menu can have at most one default menu item
Method 1: getmenu ()-> getsubmenu (0)-> setdefaultitem (1, true); // Index
Method 2: getmenu ()-> getsubmenu (0)-> setdefaultitem (id_file_open, false); // use the ID

 

C. Add a graphic Tag:
Method 1: Use Id
Cbitmap bitmap; // must be set as a global object
Bitmap. loadbitmap (idb_bitmap1 );
Getmenu ()-> getsubmenu (0)-> setmenuitembitmaps (id_file_new, mf_bycommand, & bitmap, & Bitmap );

Method 2: Index
Cbitmap bitmap; // must be set as a global object
Bitmap. loadbitmap (idb_bitmap1 );
Getmenu ()-> getsubmenu (0)-> setmenuitembitmaps (0, mf_byposition, & bitmap, & Bitmap );

D. Make the menu invalid and gray

// Must be added to the constructor: m_bautomenuenable = false;
Getmenu ()-> getsubmenu (0)-> enablemenuitem (id_file_open, mf_bycommand | mf_disabled | mf_grayed );

 

E. Remove menu
Setmenu (0 );

 

F. Add menu
Cmenu menu;
Menu. loadmenu (idr_mainframe );
Setmenu (& menu );
Menu. Detach ();

 

Graphic markup size

The system obtains the bitmap mark size:
Cstring STR;
Str. Format ("x = % d, y = % d", getsystemmetrics (sm_cxmenucheck), getsystemmetrics (sm_cymenucheck ));
MessageBox (STR );

Command update mechanism

Update_command_ui message response in the menu

{
Pcmdui-> enable (false); // true and false to set whether to use or gray
Pcmdui-> setcheck (true); // set the flag to true or false.
Pcmdui-> settext ("cut"); // change the text content of the menu item

}

 

 

Dynamic menu Creation

Define several constants first:
# Define idm_menu0 0
# Define idm_menu1 1
# Define idm_menu2 2
# Define idm_menu3 3
# Define idm_item0 10
# Define idm_item1 11
# Define idm_item2 12
# Define idm_item3 13
# Define idm_item4 14
# Define idm_item5 15
# Define idm_item6 16

 

// Add a menu item after the last menu item
Bool appendmenu (uint nflags, uint nidnewitem = 0, lpctstr lpsznewitem = NULL );
Bool appendmenu (uint nflags, uint nidnewitem, const cbitmap * pbmp );

// Insert a menu item
Bool insertmenu (uint nposition, uint nflags, uint nidnewitem = 0, lpctstr lpsznewitem = NULL );
Bool insertmenu (uint nposition, uint nflags, uint nidnewitem, const cbitmap * pbmp );

// Remove a menu item
Bool removemenu (uint nposition, uint nflags );

// Delete a menu item
Bool deletemenu (uint nposition, uint nflags );

1. Create a non-popup menu without using resources.
(1) create a non-drop-down menu.
1. Create a cmenu object in the oncreate function of the window class. If you create and useProgramMain Frame window
You can also use the initinstance () function.
2. Declare a cmenu object: cmenu mymenu;
3. Call mymenu. createmenu () or mymenu. loadmenu ()
4. Call mymenu. appendmenu () or mymenu. insertmenu () several times to create one
Menu items.
5. Call mymneu. setmenu () to attach the menu to the window.
6. Call mymenu. Detach ().

Example:
Int cmywnd: oncreate (maid)
{
Cmenu mymenu;
Mymenu. createmenu ();
Mymenu. appendmenu (mf_string, idm_menu0, "file ");
Mymenu. appendmenu (mf_string, idm_menu1, "edit ");
Mymenu. appendmenu (mf_string, idm_menu2, "View ");
Mymenu. appendmenu (mf_string, idm_menu3, "help ");
Mymenu. insertmenu (idm_menu2, mf_bycommand, idm_item0, "related ");
This-> setmenu (& mymenu );
Mymenu. Detach ();
Return 0;
} // The details of each function will not be explained. It is best to see online help.
This method is to create a menu and then paste it to the window. Then, detach () is used to disassociate the menu from the mymenu object, because the mymenu object will soon be out of scope, this step is required.

(2) create a drop-down menu without using resources.
When you move the cursor over a menu entry, a program is not executed, but a program is displayed.
A drop-down menu. You need to create two menus using the preceding method. The first is when the mouse is not clicked.
The other is to act as the drop-down menu. Example:
Int cmywnd: oncreate (maid)
{
Cmenu mymenu0, mymenu1;
// The following drop-down menu
Mymenu1.createmenu ();
Mymenu1.appendmenu (mf_string, idm_item0, "copy ");
Mymenu1.appendmenu (mf_string, idm_item1, "cut ");
Mymenu1.appendmenu (mf_string, idm_item2, "Paste ");
Mymenu1.appendmenu (mf_separator, idm_item3 ,"");
Mymenu1.appendmenu (mf_string, idm_item4, "select all ");
Mymenu1.appendmenu (mf_separator, idm_item5 ,"");
Mymenu1.appendmenu (mf_string, idm_item6, "delete ");
// The menu shown below when the mouse is not clicked
// Post the drop-down menu to the first menu in the second sentence.
Mymenu0.createmenu ();
Mymenu0.appendmenu (mf_popup, (uint) mymenu1.m _ hmenu, "edit ");

This-> setmenu (& mymenu0); // post the menu to the window
Mymenu0.detach (); // required
Mymenu1.detach (); // required
Return 0;
}

2. Create a popup menu without resources.
In many programs, you only need to right-click the client area of the window, and
Menu. This is the right-click menu. We can use the cmenu class.
Creating a menu is a little more complex than creating the first menu. First, add a member change to the window class.
Volume: cmenu * mymenu2;
Then, add the menu creation statement to the window class Constructor (or oncreate () function), and then
Add the destroy menu statement to the destructor, and add the display menu to the onrbuttondown () function.
.
When creating a menu, the cmenu class object should be allocated with new.
Example:

cmywnd: cmywnd ()
{< br> // cmywnd is derived from cwnd.
// create the menu first.
mymenu2 = new cmenu;
mymenu2-> createpopupmenu ();
mymenu2-> appendmenu (mf_string, idm_item0, "copy ");
mymenu2-> appendmenu (mf_string, idm_item1, "cut");
mymenu2-> appendmenu (mf_string, idm_item2, "Paste ");
mymenu2-> appendmenu (mf_separator, idm_item3, "");
mymenu2-> appendmenu (mf_string, idm_item4, "select all ");
mymenu2-> appendmenu (mf_separator, idm_item3, "");
mymenu2-> appendmenu (mf_string, idm_item5, "delete");

}
Cmywnd ::~ Cmywnd ()
{
Mymenu2-> destroymenu (); // destroy the system resources occupied by the menu
Delete mymenu2; // destroy menu objects
}
Void cmywnd: onrbuttondown (uint nflags, cpoint point)
{
Rect;
Getwindowrect (& rect );
// Display menu
Mymenu2-> trackpopupmenu (tpm_rightalign, point. x + rect. Left, point. Y +
Rect. Top, this, null );
}

From: http://blog.csdn.net/ltag0110rtag/article/details/7368633

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.