Pop-up menu

xiaoxiao2021-04-10  481

Pop-up menu (POPMENU) Everyone is familiar, where the mouse button pops up on the Win98 desktop is the pop-up menu. Typically, pop-up menus are popped up when the mouse button is clicked, and it can be ejected at any time as needed. In the MFC of VC 5, the class named cmenu is called CMenu. Below I introduce you to the basic method of establishing a pop-up menu.

First, create a menu resource in the resource editor

Create a new menu resource, such as the ID number of the menu is IDC_POPMENU. This menu has two layers, that is, a pop-up menu item, and this menu item popping content is the content of the pop-up menu that will be established. Such as the right picture, the menu under "Magascation" is the content of the pop-up menu that will be established. In fact, the name of "Play" is not used in later operations, but VC 5 does not allow direct establishment of pop-up menus, so the first to build a "bullets" method.

As for the message mapping of each menu item, the same as the general menu.

Second, use cMenu class objects

The CMenu class has more functions, but the establishment of pop-up menus simply uses several member functions.

1, loadMenu function

Prototype: BOOL LoadMenu (uint nidResource);

Where NidResource is the ID number of the menu resource, here is the original IDC_POPMENU.

2, getSubMenu function

Prototype: cmenu * GetSubmenu (int NPOS) const;

This function is used to get a pointer to the submenu. NPOS is the number of layers, 0 is the first layer menu ... in this class.

Since we need the first layer of the first layer of the "bullets", you can get the class pointer to the first layer menu with GetSubMenu (0).

3, TRACKPOPUPMENU function

Prototype: BOOL TRACKPOPUPMENU (uint nflags, int x, int y, cWnd * pwnd, lpcRect lpRect = null);

among them:

NFLAGS is the screen coordinate property and mouse coordinate properties

Screen coordinate properties:

TPM_CENTERALIGN landscape with X hosted

TPM_LEFTALIG is laterally to align the menu in x left

TPM_RightAlign horizontally puts the menu in X right alignment

Mouse button properties (valid only when responding to WM_CONTEXTMENU messages):

TPM_LEFTBUTTON continuously press? Right-click does not pop up the menu, right-click unworthy to select menu items

TPM_RightButton continuously pressing the mouse button will pop up the menu, the mouse button can be used for the selected menu item

X, Y is the screen coordinate

The area occupied by the LPRect menu. If you are null, when the user presses the mouse button outside the menu, the menu will disappear.

Third, instance

1. When the mouse right click on the client area of ​​the program window, the program receives a WM_CONTEXTMENU message, the best time to pop up menu

Add the response function for the WM_CONTEXT message with the "Add Windows Message Handler" feature in ClassWizard. The code is as follows:

Void CMYDLG :: OnContextMenu (CWND * PWND, CPOINT POINT)

{

CMenu Menu; / / Define CMenu class object

Menu.LoadMenu (IDC_POPMENU); // Load the original menu IDC_POPMENU MENU.GETSUBMENU (0) -> TRACKPOPUPMENU (TPM_LEFTALIGN, POINT.X, POINT.Y, PWND);

/ * GetSubMenu (0) Get the first layer of the IDC_POPMENU, TRACKPOPUPMENU pops the menu into (x, y). Since it is set to TPM_LEFTALIGN, the menu is in the upper left corner of (x, y). * /}

2, at other times, pop-up menus can also be, for example, can respond to WM_LBUTTONDOWN messages. In this way, you can also pop up the menu when you click the left mock.

Add the response function for the WM_LBUTTONDOWN message with the "Add Windows Message Handler" feature in ClassWizard. The code is as follows:

Void cmfc5dlg :: ONLBUTTONDOWN (UINT NFLAGS, CPOINT)

{

CMenu Menu; // Defines cMenu class object menu.loadMenu (IDC_POPMENU); // loaded into the original menu IDC_PopMenu ClientToscreen (& Point); menu.getsubmenu (0) -> TRACKPOPUPMENU (TPM_LEFTALIGN, POINT.X, POINT.Y, THIS);

/ * GetSubMenu (0) Get the first layer of the IDC_POPMENU, TRACKPOPUPMENU pops the menu into (x, y). Since it is set to TPM_LEFTALIGN, the menu is in the upper left corner of (x, y). * /

ScreenToclient (& POINT);

CDIALOG :: ONLBUTTONDOWN (NFLAGS, POINT);

}

Note: The coordinates stored in the Point object stored in the WM_LButtondown message are relative to the window client area, and the X, Y in the TRACKPOPUPMENU needs to be relative to the screen, so you need to convert with the ClientToscreen function, but this message response function is to be called CDIALOG :: ONLBUTTONDOWN (NFLAGS, POINT), so you should use the ScreenToClient function to restore the coordinates stored in POIN to relative to the window customer area.

转载请注明原文地址:https://www.9cbs.com/read-133311.html

New Post(0)