The previous article showed how tu use a WH_MSGFILTER hook in order to make a menu with multiple selection. Now, let’s see an MFC-extension class that implements a context pop-up menu with multiple selection.
class CMultiSelPopupMenu : public CMenu
CMultiSelPopupMenu(UINT nIDResource, UINT nSubMenu);
void MDX_Check(CDataExchange* pDX, WORD wItemID, BOOL& bCheck);
BOOL Show(UINT nFlags, CPoint point, CWnd* pWnd);
CMultiSelPopupMenu class is derived from CMenu MFC class and has three public methods:
- CMultiSelPopupMenu::CMultiSelPopupMenu constructs a CMultiSelPopupMenu object.
- nIDResource – resource identifier of the menu;
- nSubMenu – the index of pop-up sub-menu to be shown.
- CMultiSelPopupMenu::MDX_Check looks very similar to DDX_Check global function from the MFC framework. While DDX_Check is used to manage data exchange between check box controls and window class objects, CMultiSelPopupMenu::MDX_Check manages data exchange between multiple-selection menus and window class objects.
- pDX – a pointer to a CDataExchange object; it has the same purpose as in DDX_Check;
- wItemID – the identifier of the menu item;
- bCheck – a reference to a member variable of the window class object with which data is exchanged.
- CMultiSelPopupMenu::Show shows a pop-up context menu. It is similar to CMenu::TrackPopupMenu, except that shown menu allows multipe selection.
- nFlags – specifies screen-position and mouse-position flags; see TrackPopupMenu documentation for more details;
- point – screen coordinates of the pop-up menu;
- pWnd – the window that owns the pop-up menu.
You can get more usage and implementation details from the attached demo project. I think, the source code can tell more than any other explanations. However, if something isn’t very clear, please do not hesitate to leave a reply.
The demo project is a simple MFC application that shows a multiple-selection context menu when the user right-clicks in the client area of the window. Click on items to check/uncheck the desired ones. Finally, to dismiss the menu just click outside.
- Codexpert blog: Multiple Selection Menu – Part 1