3

I have a window with a listbox which I would like to right click an entry in the list box and have certain options displayed in a context menu. I've looked online but it seems as if I can only get examples in MFC C++, or simply c#.

Is this possible in standard Win32 c++? I can handle a right click on the listbox via the WM_CONTEXTMENU message, but how can I make a menu appear?

Mat
  • 202,337
  • 40
  • 393
  • 406
Jim Newtron
  • 227
  • 5
  • 11
  • Perhaps related to [this](http://stackoverflow.com/questions/12796501/detect-clicking-inside-listview-and-show-context-menu) question? Also isn't using `TrackPopupMenu` a possibility in this case? – Bhargav Dec 22 '12 at 09:06

1 Answers1

7

I figured it out, thanks to Bhargav Bhat's comment about the related question.

Handle right click on the listbox via checking for WM_CONTEXTMENU in your WndProc.

Grab the handle to the window via the wParam parameter, compare it to your listbox to see if the user right clicked the listbox.

From there, create the popupmenu via CreatePopupMenu().

Insert/Append into the menu via InsertMenu()/AppendMenu().

Finally, call TrackPopupMenu().

case WM_CONTEXTMENU:
        if ((HWND)wParam == m_hListBox) {
            m_hMenu = CreatePopupMenu();
            InsertMenu(m_hMenu, 0, MF_BYCOMMAND | MF_STRING | MF_ENABLED, 1, "Hello");
            TrackPopupMenu(m_hMenu, TPM_TOPALIGN | TPM_LEFTALIGN, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, m_hWnd, NULL); 
        }
Jim Newtron
  • 227
  • 5
  • 11