Rather than relying on the WM_RBUTTON(DOWN|UP)
messages to determine the mouse coordinates, the WM_CONTEXTMENU
‘s own 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 ScreenToClient()
or MapWindowPoints()
.
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 lParam
of 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_GETITEMPOSITION
or LVM_GETITEMRECT
as needed, then convert that position to screen coordinates using ClientToScreen()
or MapWindowPoints()
, and then display the popup menu at that screen location.
CLICK HERE to find out more related problems solutions.