Add a right-click menu in the (CLISTVIEW) list view

xiaoxiao2021-03-06  48

Hello everyone, this is my first article, use my most common list view to start, the function I want to do is to add a right-click menu on the list box, and prompt the currently selected text content, function is very Simple, and it is adapted on the basis of others. However, everything starts from the simplest thing. "The world is difficult, it must be easy; the world is big, it must be fine", and only standing in others The shoulders can only look further, J, idle, and book regulations.

In the CodeProject website (http://www.codeproject.com/listctrl/drivebrowser.asp), the example of barretto VN has given an example of display (specific) drive with the list box, so the job now to do is Learn more than those related to right-click menu should be added to that location.

Use pop-up menus (POPMENU)

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, of course, can also 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.

1. Create a menu resource in the resource editor to create a new menu resource, such as the ID number of the menu is IDC_POPMENU. This menu has two layers, which has a pop-up menu item, and this menu item is the content of the pop-up menu that will be created. As for the message mapping of each menu item, the same as the general menu. Second, using the CMenu class object CMenu class member function, but establishing pop-up menus only need to use several member functions. 1, loadMenu function

Function: Load menu resource prototype from the application's executable: BOOL LoadMenu (uint nidResource); where NidResource is the ID number of the menu resource, here is the original IDC_POPMENU. 2, getSubMenu function

Function: This function is used to get a pointer to the submenu. Prototype: cmenu * getSubmenu (int NPOS) const; npos is a layer, 0 is the first layer menu ... With this type. 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

Function: Display the pop-up menu in the specified location and track the selected key: BOOL TRACKPOPUPMENU (Uint NFLAGS, INT X, INT Y, CWND * PWND, LPCRect LPRECT = NULL); where: NFLAGS is the screen coordinate property and mouse coordinate properties Screen coordinate properties: TPM_CENTERALIGN Landscape Menu TPM_LEFTALIGN translators in X left align TPM_RightAlign horizontally landscape with x right alignment mouse button attribute (only when responding to WM_CONTEXTMENU messages): TPM_LEFTBUTTON continuously press? Right button does not pop up menu Right-click the selected menu item TPM_RightButton continuously pressing the mouse to pop up the menu, the mouse button can be used for the selected menu item x, y is the area of ​​the screen coordinate LPRECT menu. If null, when the user presses the mouse button outside the menu, the menu will disappear. The specifically implements the response function to the NM_RClickT message with the "Add Windows Message Handler" function in ClassWizard. The code is as follows. In detail, the debugging in the VC6 Win2000 environment, you are interested in yourself try yourself: void cmyview :: Onrclick (nmhdr * pnmhdr, lresult * pResult)

{

CListCtrl & clist = getListCtrl (); // Get pointers for current list controls

CMenu Menu, * psubmenu; // Defines the CMENU object you want to use.

Menu.loadMenu (idR_popmenu); // Load Custom Right-click Menu

psubmenu = menu.getsubmenu (0); // Get the first pop-up menu, so the first menu must have a submenu

CPoint OPOINT; / / Define a location for determining the position of the cursor

GetCursorpos (& OPOINT); / / Get the position of the current cursor so that the menu can follow the cursor

INT ISTAT = Clist.getSelectionMark (); // Store the currently selected in ISTAT is the first few

CString Pstring = Clist.getItemText (ISTAT, 0); // Get data in the current item, 0 representative is the 0th list

pstring = "The path you choose is:" pstring; // Display the current selection

MessageBox (PString); // Displays the currently selected path

Psubmenu-> TrackPopupMenu (TPM_LEFTALIGN, OPOINT.X, OPOINT.Y, this); // Display the pop-up menu in the specified location

}

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

New Post(0)