I'm trying to create a shortcut menu, to give a list of options when
the user presses the right mouse button. It's not quite working -
hopefully it is something simple... The code is adapted from MSDN.
I have two sorts of window - a main window and notes. These each have
different shortcut menus.
In the window function I have:
case WM_CONTEXTMENU:
if (!OnContextMenu(hWnd, LOWORD(lParam), HIWORD(lParam),
IsMainWindow))
return DefWindowProc(hWnd, message, wParam, lParam);
break;
and I also have two further functions as follows:
BOOL WINAPI OnContextMenu(HWND hwnd, int x, int y, BOOL IsMain) {
RECT rc; // client area of window
POINT pt = { x, y }; // location of mouse click
// Get the bounding rectangle of the client area.
GetClientRect(hwnd, &rc);
// Convert the mouse position to client coordinates.
ScreenToClient(hwnd, &pt);
// If the position is in the client area, display a
// shortcut menu.
if (PtInRect(&rc, pt)) {
ClientToScreen(hwnd, &pt);
DisplayContextMenu(hwnd, pt, IsMain);
return TRUE; }
// Return FALSE if no menu is displayed.
return FALSE;
}
VOID APIENTRY DisplayContextMenu(HWND hwnd, POINT pt, BOOL IsMain) {
HMENU hmenuTrackPopup; // shortcut menu
LPCTSTR menuname;
// Load the menu resource.
menuname = IsMain ? _TEXT("MainPopupMenu") : _TEXT("NotePopupMenu");
if ((hmenuTrackPopup = LoadMenu(hInst, menuname)) == NULL)
return;
// Display the shortcut menu. Track the right mouse
// button.
TrackPopupMenu(hmenuTrackPopup,
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
pt.x, pt.y, 0, hwnd, NULL);
// Destroy the menu.
DestroyMenu(hmenuTrackPopup);
}
and in the RC file I have:
MainPopupMenu MENU
BEGIN
MENUITEM "E&xit", IDM_EXIT
END
NotePopupMenu MENU
BEGIN
MENUITEM "&About ...", IDM_ABOUT
END
When I press (or rather, when I release) the right mouse button some
rectangles appear on the screen, but they don't look very much like
the menu I was expecting, in particular they are nothing like as wide.
However, clicking on them with the mouse does operate "Exit" or
"About" as appropriate, so the menus do seem to be there in some
sense. Also, in an earlier attempt, in which WM_CONTEXTMENU simply put
up a dialog box, this would respond to pressing the "Menu" key on the
keyboard, but this does not now appear to work.
I expect it's all something simple, but can anyone help?
Thanks very much.
Paul.