Displaying a Shortcut Menu

HomeOverviewHow Do I

It’s recommended that your Windows 95 application use the right mouse button for displaying a shortcut menu, providing easy access to the most commonly used commands. What commands you offer on your shortcut menu will depend on whether the menu is invoked for a particular window, a particular object, and so on. See Windows Interface Guidelines for Software Design on the MSDN Library CD for guidelines on what kind of commands you should offer.

You can use the Gallery to add a shortcut menu that applies to a given view class in your application. However, DRAWCLI displays the shortcut menu only when the right button is clicked over a selected object. Depending on your own application’s needs, you can use the shortcut menu offered by the Gallery as is, you can make modifications as required, or you can add one manually.

The DRAWCLI sample provides a shortcut menu containing several commands from the Edit menu.

To add a shortcut menu to your application, first use the Visual C++ menu editor to create a menu bar without a title, and then define the shortcut menu as the first menu.

To display the shortcut menu, your application needs a handler for the WM_CONTEXTMENU message. You can use ClassWizard to make the following changes to your source files:

You then fill in the definition of OnContextMenu. Here’s DRAWCLI’s implementation of CDrawView::OnContextMenu:

void CDrawView::OnContextMenu(CWnd* /*pWnd*/, CPoint point) 
{
   // make sure window is active
   GetParentFrame()->ActivateFrame();

   CPoint local = point;
   ScreenToClient(&local);
   ClientToDoc(local);

   CDrawObj* pObj;
   pObj = GetDocument()->ObjectAt(local);
   if (pObj != NULL)
   {
      if (!IsSelected(pObj))
         Select(pObj, FALSE);
      UpdateWindow();

      CMenu menu;
      if (menu.LoadMenu(ID_POPUP_MENU))
      {
         CMenu* pPopup = menu.GetSubMenu(0);
         ASSERT(pPopup != NULL);

         pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
            point.x, point.y,
            AfxGetMainWnd()); // use main window for cmds
      }
   }
}

DRAWCLI displays a pop-up menu only when the right mouse button is clicked over an object, so this function first checks whether an object lies where the mouse event occurred, and if so, the function selects it. (OnContextMenu receives the location of the mouse event in screen coordinates, not in client coordinates like handlers such as OnLButtonDown. Accordingly, the function converts the coordinates using CWnd::ScreenToClient before doing hit-testing.) The function then loads the ID_POPUP_MENU menu and calls CMenu::GetSubMenu to get its first sub-menu. Finally, the function calls CMenu::TrackPopupMenu on the submenu to display it as a pop-up.

Note that, as long as the pop-up menu commands are duplicates of commands you’ve defined elsewhere, there’s no need to define any new ON_COMMAND or ON_UPDATE_COMMAND_UI macros.

You can also support shortcut menus in the Windows 95 shell. A shortcut menu that offers basic commands such as Delete and Rename is available for all files in the shell. Some commands, such as Open or Print, are enabled only if there is an application associated with the file’s extension. You can enable these commands for files created by your MFC application by calling CWinApp::RegisterShellFileTypes, passing TRUE as a parameter; the parameter is needed for shell registration to work properly under Windows 95. (AppWizard inserts a call to this automatically if you specify a file name extension for your files.) This function adds entries to the system registry that let the shell invoke your application to open or print a file. You can also write a shell-extension DLL that customizes the shortcut menu for your application’s files; for more information, see Context Menu Handlers on the MSDN Library CD.