Popup menus These are the steps necessary to produce a popup menu that will appear when you right click on a control. Task 1: Adding popup menus 1. Create a menu resource. 2. Your popup menu will be the submenu. Menu headers are ignored. Create your menu with some number of entries You can add a popup to most any control. We'll just assume it's a generic CWnd object you'll be adding the popup to. 3. In ClassWizard, add a handler for right clicks. 4. In your right click handler. Add a CMenu local variable. From your CMenu variable, call LoadMenu() to get the menu resource. 5. Call GetSubMenu(0) A CMenu pointer will be returned. This is your popup menu. 6. Have your popup menu call TrackPopupMenu(). You should now see a popup menu. Optional: This code is inefficient. The menu is constant and only needs to be loaded once. If you want to optimize your code, direct OnInitDialog() or OnCreate() call some code to do LoadMenu() and GetSubMenu(). Alternative: The steps described only produce a static popup menu. If you want a dynamic one, call CreateMenu(), AppendMenu() and TrackPopupMenu() to create your popup menu. Task 2: Getting the user input via the popup menu You've got a CWnd with a popup menu. That's nice, but it would be better if we could get the user's selection on the popup menu. No problem. Here's how. 1. Open ClassWizard, ClassName: your CWnd class. 2. In your Object ID's list, there should be entries for each of your popup's menu items. Select one of these Object ID, map it to COMMAND in the messages box. This should produce a handler in your CWnd class that will be called whenever the user selects the associate menu item. 3. Map all the Object ID popup menu items to their COMMAND message Alternative: The author can not confirm this since he has not tried this before. If you want to get input from dynamic popup menus, use TPM_NONOTIFY (possibly also TPM_RETURNCMD). The return value will be the ID of the menu item you set when you called AppendMenu(). Notes: You may want to stay away from dynamic menus. Dynamic menus that change contents based on the control can be confusing for a user. Menu items that appear and disappear on context may look cool, but may mystify the user when the user doesn't see an item he expects. Instead use a static menu, but enable/disable the menu items. Notes: An improvement to this lab is to use WM_CONTEXTMENU instead of WM_RBUTTONDOWN. WM_CONTEXTMENU can track the right mouse click as well as shift-F10 and context menu key (105-key keyboard).