Tech Support > Microsoft Windows > Development Resources > Shortcut menu
Shortcut menu
Posted by gw7rib@aol.com on May 8th, 2007


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.

Posted by David Lowndes on May 8th, 2007


Paul,

Try adding this:

HMENU hPop = GetSubMenu( hmenuTrackPopup, 0 );

and use that popup menu subsequently with the TPM api.

Dave

Posted by gw7rib@aol.com on May 9th, 2007


On 9 May, 00:18, David Lowndes <Dav...@example.invalid> wrote:
Thanks Dave. I modified the code as you suggested, and also altered
the RC file to make the menu I wanted a sub-menu rather than the main
menu. With these changes I got it working. It seems strange that you
can't put in a menu and access it directly, it needs to be a submenu,
but there you go...

I also sorted out the the "Menu key" problem - reading the
instructions for WM_CONTEXTMENU shows that x and y are -1 if you call
up the menu from the keyboard. Once you know this (and allow for the
fact that they seem to sneakily change value to 65535) it works fine.

Thanks for your help.
Paul.



Similar Posts