Rather than relying on the
WM_RBUTTON(DOWN|UP) messages to determine the mouse coordinates, the
lParam gives you the mouse’s screen coordinates of the message that generated the
WM_CONTEXTMENU. If those coordinates are not what you are expecting, you can use
GetMessagePos() instead, which will report the screen coordinates at the time
WM_CONTEXTMENU was generated. Either way, you can then convert the screen coordinates into ListView client coordinates using
Just be sure you also handle the case where the popup menu is being invoked by the user via keyboard input rather than mouse click. In that case, the
WM_CONTEXTMENU will carry the screen coordinates
[x=-1,y=-1], and you can query the ListView for the position of its selected item(s) using
LVM_GETITEMRECT as needed, then convert that position to screen coordinates using
MapWindowPoints(), and then display the popup menu at that screen location.
CLICK HERE to find out more related problems solutions.