Vc mfc menu

Source: Internet
Author: User
Vc mfc menu

Menu item attribute description:
Id identifies the unique constant of the menu.
The title of the caption menu item. The characters after "&" are shortcut keys. The characters after the menu item are underlined.
Separator horizontal line. Other attributes are invalid.
Pop_up has sub-menu
Invalid grayed. The title is displayed in gray.
Inactive is invalid. The title is displayed normally.
Checked adds a check before the title
When break is set to none, it is displayed in one row or column with its sibling.
Help is only valid for top menu items so that it and the top menu at the end are moved to the upper right corner of the window.
Prompt prompt when you point to it
In addition to generating idr_mainframe, multi-document applications also generate idr_xxxxtype, where XXXX is the application name. They correspond to no document and no document respectively.
A menu ID can have a response function in multiple classes, but only one response is available.
The response sequence of my experiment results, cchildframe, ccmenuapp, ccmenudoc, ccmenuview, and cmainframe (my application name is cmenu) is:
In idr_mainframe, the other three of cmainframe and ccmenuapp do not respond.
In idr_xxxxtype, ccmenuview, ccmenudoc, cchildframe, ccmenuapp, and cmainframe.
CTRL + W open classwizard, select the project, class (xxxview is the most commonly selected), and select the menu item to be modified in the ID.
Double-click Command (or update_command_ui) and click OK. Double-click the added function in the member function to edit the function.
When you click the menu, the function we just edited will be executed.
If you double-click updata_command_ui, the response format is similar to the following:
Void... onupdate... (ccmdui * pcmdui)
{
Pcmdui-> setcheck (true); // Add a check before the menu item
Pcmdui-> enable (true); // enables the menu item to be used
}
Because this function often affects its shape, it will be executed when its "father" or "grandfather" is selected.

Functions related to menus in the cwnd class.
Setmeun (cmenu * pmenu );
Modify the window menu. If it is null, it indicates deletion.
Frequently Used
Getmenu ();
Getsystemmenu ();

Cmenu functions.
The appendmenu () function adds a new menu item at the end of the menu. You can also specify the relevant information of the menu item. It has two syntaxes.
The nflag specifies the status, which can be one of the following four groups or a combination, or a combination with mf_popup to indicate that a pop-up menu is added.
Mf_checked, mf_checked
Mf_disabled, mf_enabled, mf_grayed
Mf_string, mf_ownerdraw, mf_separator, and mf_bitmap menu items are strings, self-drawn, separator, and bitmap.
Mf_menubarbreak, mf_menubreak
Nidnewitem specifies the ID of the menu item.
Lpsznewitem specifies the content of the menu item, which is related to nflag. When mf_ownerdraw is used, this parameter is a Data Pointer used to transmit data. The system saves the data to the parameter when sending messages wm_measureitem and wm_drawitem.
(Drawitemstruct structure) itemdata field; When nflag is mf_string, this parameter is the menu title.
Insertmenu
Nflags specifies the position and status of menu items. For details about the status options, see the appendmenu () function. The position options are mf_bycommand and mf_byposition.
If npositin is mf_bycommand, the new menu item is inserted before the specified menu item; or mf_byposition. This parameter specifies the position of the new menu item, which is-1 inserted to the end.
The modifymenu () parameter is similar to the insertmenu parameter.
Deletemenu Delete menu items
Removemenu remove menu items

Set and display floating menu
Bool trackpopupmenu (uint nflags, int X, int y,
Cwnd * pwnd, lpcrect = NULL );
The nflag floating menu coordinate setting method and mouse operation method. Valid values are as follows:
Tpm_centeralign tpm_leftalign tpm_rightalign
Tpm_leftbutton tpm_rightbutton
X, Y floating menu coordinates
The window of the Operation menu specified by pwnd
Lprect specifies the range of mouse operations
Click the left button in the customer area to bring up a shortcut menu. method 2 needs to edit a new menu in the resource editor, and method 3 must have a main menu.
Method 1:
Void cheview: onlbuttondown (uint nflags, cpoint point)
{
Cmenu popupmenu;
Popupmenu. createpopupmenu ();
Popupmenu. appendmenu (mf_string, id_file_new, "new ..");
//...

Clienttoscreen (& Point );
Popupmenu. trackpopupmenu (tpm_centeralign | tpm_rightbutton, point. X, point. Y, this );

Cview: onlbuttondown (nflags, point );
}

Method 2:
Void cheview: onlbuttondown (uint nflags, cpoint point)
{

Cmenu menu;
Menu. loadmenu (idr_dummy );
Cmenu * pmenu = menu. getsubmenu (0 );
Assert (pmenu! = NULL );

Clienttoscreen (& Point );
Pmenu-> trackpopupmenu (tpm_centeralign | tpm_rightbutton, point. X, point. Y, this );
Cview: onlbuttondown (nflags, point );
}

Method 3:
Void cheview: onlbuttondown (uint nflags, cpoint point)
{
Cwnd * pwnd = afxgetapp ()-> getmainwnd ();
Cmenu * pmenu = pwnd-> getmenu ();
Pmenu = pmenu-> getsubmenu (0 );
Assert (pmenu! = NULL );

Clienttoscreen (& Point );
Pmenu-> trackpopupmenu (tpm_centeralign | tpm_rightbutton, point. X, point. Y, this );

}
Exercise:
Dynamic menu. Click "More menu" to add some menu items.
In fact, it is difficult to understand the principle of the self-painting menu. The appendmenu style is customized, and the information (pointer) used for the Self-painting is forcibly converted to the lpctstr and then duplicated.

Load drawitem. Note that do not delete the information used for self-painting in advance.
Example:
In cownermenu. h
Class cmenuitem
{
Public:
Cstring m_sztext;
Colorref m_color;
Cmenuitem (cstring sztext, colorref color)
{
M_sztext = sztext;
M_color = color;
}

};

# Include <afxtempl. h>

Class cownermenu: Public cmenu
{
Public:
Void drawitem (lpdrawitemstruct );
Bool appendmenu (uint nidnewitem, cstring caption, colorref color );
Cownermenu ();

Ctypedptrlist <cptrlist, cmenuitem *> m_menulist;
Virtual ~ Cownermenu ();

};

In cownermenu. cpp
Cownermenu ::~ Cownermenu ()
{
While (m_menulist.getcount ())
{
Cmenuitem * pmenuitem = m_menulist.gethead ();
Delete pmenuitem;
M_menulist.removehead ();
}
}

Bool cownermenu: appendmenu (uint nidnewitem, cstring caption, colorref color)
{
Cmenuitem * pmenuitem = new cmenuitem (Caption, color );
M_menulist.addtail (pmenuitem );

Return cmenu: appendmenu (mf_ownerdraw, nidnewitem, (lpctstr) pmenuitem );
}

Void cownermenu: drawitem (lpdrawitemstruct)
{
CDC * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC );
PDC-> settextcolor (cmenuitem *) lpdrawitemstruct-> itemdata)-> m_color );
PDC-> textout (0, 0, (cmenuitem *) lpdrawitemstruct-> itemdata)-> m_sztext );

}

When using the menu (do not forget to add the header file ):
Void cownermenuview: onrbuttondown (uint nflags, cpoint point)
{
Cownermenu menu;
Menu. createpopupmenu ();

Menu. appendmenu (id_1, "1", RGB (0, 0, 128 ));
Clienttoscreen (& Point );

Menu. trackpopupmenu (tpm_leftbutton | tpm_leftalign, point. X, point. Y, this );
Cview: onrbuttondown (nflags, point );

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.