You are familiar with the pop-up menu (popmenu). On the Win98 desktop, right-click the pop-up menu and choose pop-up menu. In general, the pop-up menu pops up when you right-click it. Of course, it can also pop up at any time as needed. In the MFC of VC ++ 5, the management menu class is cmenu. The following describes how to create a pop-up menu.
1. Create a menu resource in the resource editor
Create a menu resource. For example, set the menu ID to idc_popmenu. This menu has two levels, that is, a menu item that can be popped up, And the pop-up content of this menu item is the content of the pop-up menu to be created. As shown in the right figure, the menu under "pop-up items" is the content of the pop-up menu to be created. In fact, the "pop-up items" name will not be used in future operations, but the pop-up menu cannot be directly created in VC ++ 5, therefore, we should first establish the "pop-up items" method.
Message ing for each menu item is the same as that for a general menu item.
Ii. Use cmenu class objects
Cmenu class has many member functions, but only a few member functions are needed to create a pop-up menu.
1. loadmenu Function
Prototype: bool loadmenu (uint nidresource );
Here, nidresource is the ID of the menu resource, and the newly created idc_popmenu is used.
2. getsubmenu Function
Prototype: cmenu * getsubmenu (INT NPOs) const;
This function is used to obtain the sub-menu pointer. NPOs is the number of layers, and 0 is the first sub-menu ...... And so on.
Because we need the first sub-menu of "pop-up items", we use getsubmenu (0) to get the class pointer of the first sub-menu.
3. trackpopupmenu Function
Prototype: bool trackpopupmenu (uint nflags, int X, int y, cwnd * pwnd, lpcrect lprect = NULL );
Where:
Nflags is the screen coordinate and mouse coordinate attributes.
Screen coordinate attributes:
Tpm_centeralign center the menu horizontally by X
Tpm_leftalign align the menu horizontally to the left of X
Tpm_rightalign align the menu horizontally to the right of X
Mouse button properties (only valid when responding to the wm_contextmenu message ):
Tpm_leftbutton continuous press? The right-click menu does not pop up continuously, and the right-click menu cannot be used to select a menu item
The right-click tpm_rightbutton continuously pops up the menu. You can right-click the button to select a menu item.
X and Y are screen coordinates.
The area occupied by the lprect menu. If it is null, the menu disappears when you press the mouse button outside the menu.
Iii. Instances
1. When you right-click the customer area in the program window, the program will receive a message wm_contextmenu, which is the best time to bring up the menu.
Use the "add windows message handler" function in classwizard to add a response function for the wm_context message. The code in the function is as follows:
Void cmydlg: oncontextmenu (cwnd * pwnd, cpoint point)
{
Cmenu menu; // defines a cmenu Class Object
Menu. loadmenu (idc_popmenu); // load the newly created menu idc_popmenu menu. getsubmenu (0)-> trackpopupmenu (tpm_leftalign, point. X, point. Y, pwnd );
/* Getsubmenu (0) gets the first sub-menu of idc_popmenu. trackpopupmenu pops up the menu to (x, y. Because it is set to tpm_leftalign, the menu is in the upper left corner (x, y. */
}
2. Other pop-up menus are also supported. For example, you can respond to the wm_lbuttondown message. In this way, the menu can also be displayed when the left mouse button is clicked.
Use the "add windows message handler" function in classwizard to add a response function for the wm_lbuttondown message. The code in the function is as follows:
Void cmfc5dlg: onlbuttondown (uint nflags, cpoint point)
{
Cmenu menu; // defines the cmenu Class Object menu. loadmenu (idc_popmenu); // load the newly created menu idc_popmenu clienttoscreen (& Point); menu. getsubmenu (0)-> trackpopupmenu (tpm_leftalign, point. x, point. y, this );
/* Getsubmenu (0) gets the first sub-menu of idc_popmenu. trackpopupmenu pops up the menu to (x, y. Because it is set to tpm_leftalign, the menu is in the upper left corner (x, y. */
Screentoclient (& Point );
Cdialog: onlbuttondown (nflags, point );
}
Note: The coordinates of the point object obtained in the wm_lbuttondown message are relative to the window client, while the x and y in trackpopupmenu must be relative to the screen, so the clienttoscreen function is required for conversion, however, the message response function calls cdialog: onlbuttondown (nflags, point). Therefore, use the screentoclient function to restore the coordinates of the point to those of the window client.