menu
He Zhidan
Menu Item Properties Description: The only constant of the ID identifies the menu. The Caption menu item title, "&" The characters behind "&" are shortcuts, and the characters after the menu item will be marked. Separator horizontal line, other properties are invalid. POP_UP has a submenu Grayed invalid, the title is invalid in gray, the title is normal to display Checked when adding a pair of Hook Break to None before the title, making it a line or a column of its brothers or one. Help is only valid for the top-level menu item, so that it and the last top menu later moves to the upper right corner of the window. PROMPT When the mouse pointing to it, the multi-document application generates an IDR_XXXXXTYPE in addition to generating IDR_MAINFRAME, where xxxx is an application name. They correspond to no documentation and documentation, respectively. A menu ID can have a response function in multiple classes, but there will be only one response. I test the results, CChildFrame, CCMenuApp, CCMenuDoc, CCMenuView, CMainFrame (my application called CMenu) in response to the order: in IDR_MAINFRAME in, CMainFrame, CCMenuApp other three do not respond in IDR_XXXXTYPE in, CCMenuView, CCMenuDoc. , Cchildframe, ccMenuapp, cmainframe.ctrl w Open ClassWizard, select the project, class (most common is xxxView), select the menu item we want to modify in the ID. Double-click the Command (or Update_Command_ui) to determine it. In the member function, double-click the function we just added, you can edit the function. When the user clicks on the menu, the function we just edited will execute. If we double-click UPDATA_COMMAND_UI, the response is similar as follows: void ... onupdate ... (ccmdui * pcmdui) {pcmdui-> setcheck (true); // Add a pair of hook pcmdui-> enable before menu item True); // Make the menu item to use} Because this function often affects its shape, it will be performed when its "father" or "grandfather" is selected.
Several functions related to the menu in the CWND class. Setmeun (cMenu * Pmenu); Modify the menu of the window, to null, is deleted. Commonly used GetMenu (); GetSystemMenu ();
Some functions of CMenu. The menu specified by the appendmenu () function is finally attached to a new menu item, and you can specify the relevant case of the menu item, which has two syntax. NFLAG specifies the state, which may be or in combination with one of the following four groups, and can also be added to the MF_POPUP combination to the pop-up menu. MF_CHECKED, MF_CHECKEDMF_DISABED, MF_ENABED, MF_G_G_WARD_STRING, MF_OWNERDRAW, MF_SEPARATOR, MF_BITMAP menu items are string, self-drawn, separated, bitmaps. MF_MENUBARBREAK, MF_MENUBREAKNIDNEWITEM Specifies the content of the menu item ID.lpsznewItem specifies the content of the menu item, which is related to NFLAG. When MF_OWNERDRAW, this parameter is a data pointer to transmit data, and the system stores the data into the parameter (DrawItemStruct structure) ItemData field when sending a message WM_MeasureItem and WM_DRAWITEM; NFLAG is MF_String This parameter is a menu title. INSERTMENUNFLAGS Specifies the list of menu items and status, and the status option will see the appendmenu () function. If the location option is MF_BYCOMMAND, if the mf_bycommand, the new menu item is inserted before the specified menu item; or MF_BYPOSITION, this parameter specifies the location of the new menu item Insert to -1 in the last. ModifyMenu () parameters are similar to INSERTMENU. DeleteMenu Removes menu item RemoveMenu Removes menu item Settings and Displays Floating Menu Bool TrackPopUpMenu (uint NFLAGS, INT X, INT Y, CWND * PWND, LPCRECT = NULL); NFLAG Floating Menu Coordinate Settings and Mouse Operation Mode, Valid Value As follows: TPM_CENTERALIGN TPM_LEFTALIGN TPM_RIGHTALIGNTPM_LEFTBUTTON TPM_RIGHTPM_LEFTBUTTON TPM_RIGHTBUTTONX, Y Floating Menu Coordinate PWND Specify Operation Menu Window LPRECT Specifies the Mouse Operation Range to pop up the shortcut menu, the method II requires editing a new menu in the resource editor, method The third must have a main menu. Method 1: Void Cheview :: ONLBUTTONDOWN (uint nflags, cpoint point) {cmenu popupnu; 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) {
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, users click "More Menu" to add some menu items. In fact, the principle of self-painting menu is not difficult to understand. The style of the .appendmenu is selected to be painted, and the information (pointer) of self-painted use is forced into LPCTSTR, and then
Carrier DrawItem on the line, note the information from Videos used not advance delete the examples are as follows: COwnerMenu.h the class CMenuItem {public:. CString m_szText; COLORREF m_color; CMenuItem (CString szText, COLORREF color) {m_szText = szText; m_color = color; }
}
#include
Class CownerMenu: Public CMenu {public: Void DrawItem (LPDrawItemstruct LPDrawItemstructure); Bool appendmenu (uint NidnewItem, CString Caption, ColorRef Color); CownerMenu (); COWNERMENU ();
Ctypedptrlist
}
COWNERMENU :: ~ cownermenu () {while (m_menulist.get ()) {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 lpDrawItemStruct) {CDC * pDC = CDC :: FromHandle (lpDrawItemStruct-> hDC); pDC-> SetTextColor (((CMenuItem *) lpDrawItemStruct-> itemData) -> m_color); pDC-> TextOut ( 0,0, (cMenuitem *) lpdrawitemstruct-> itemdata) -> m_sztext);