Popup menu on mousemove message
Hi,
I am trying to implement a popup menu that opens on a mousemove message rather than a right mouse click. When the cursor is in a particular region of the client window, the popup menu should be displayed. If the mouse moves to a different region, the original popup menu should 'disappear' and a modified menu should appear that is specific to that region.
I am finding this quite difficult to get working - does anyone have any ideas for me? This has been my approach so far:
In the OnMouseMove handler - I check the cursor coordinates and create/load the popup menu specific to the region. Because I am using TrackPopupMenu to display the popup menu, no other mouse move messages were getting through until I closed that menu or select an item from it.
My attempt at a solution (after searching on CodeGuru) was to use a thread specific hook to catch the mousemove message - to do this I used:
Code:
SetWindowsHookEx(WH_MOUSE, MouseHook, NULL,GetCurrentThreadId());
The MouseHook function is declared as:
Code:
LRESULT CALLBACK MouseHook(int nCode, WPARAM wParam, LPARAM lParam)
In this function I check if it is a mousemove message and then call the same code to check the region and create a new popup menu.
My problem is that the popup menu will display the first time and then will remain on the screen until I close it or select an item from it .... if I step into the code, the MouseHook function is being called and sometimes fires the code to create a new menu, but it doesn't seem to work. I am not sure if my problem lies in how I am implementing the creation of the menu or how I am trying to create and use the windows hook.
Sorry for the lengthy explanation, but can anyone shed some light on this problem? Or perhaps suggest a better way to achieve the same result??
thanks,
Em
Re: Popup menu on mousemove event
Quote:
Originally posted by Emster
Because I am using TrackPopupMenu to display the popup menu, no other mouse move events were getting through until I close that menu or select an item from it.
To clarify you should refer to handling mouse messages not events. Events are handled in VB.
Anytime menu pops up menu enters its modal loop and captures mouse. Roger’s guess is correct. You have to dismiss menu somehow before you invoke another one.
GetLastError returns 0x000005A6 - Popup menu already active.
Quote:
Originally posted by Emster
My attempt at a solution (after searching on CodeGuru) was to use a thread specific hook to catch the mousemove event - to do this I used:
.
.
. In this function I check if it is a mousemove event and then fire the same code to check the region and create a new popup menu.
this approach is correct. It allows you to catch WM_MOUSEMOVE message and act on it. Again nothing is fired here this is C++ MFC programming - mouse message is being sent to a window and you catch it inside of the hook.
Quote:
Originally posted by Emster
My problem is that the popup menu will display the first time and then will remain on the screen until I close it or select an item from it
You can use several approaches to dismiss menu.
Some coding: call SendInput. You with INPUT dwFlags set to MOUSEEVENTF_LEFTDOWN. You would have to make sure that cursor is not in menu area.
The simplest. Use PostMessage(WM_LBUTTONDOWN, 0, 0); it always works.
You can also use FindWindow to find menu and DestroyWindow to dismiss menu.