Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Long story short:

I needed to spy on an application so as to detect File,Save click.
I have had help both here and over at codeguru and now at the end of the week, I can do what I
set out to do. using an application hook (not a global one).

The problem I am having is sampling WM_MENUSELECT causes a "true" condition just by cruising
the mouse over the sub-menu (ie &Save). obviously this is unacceptable as I need to know when the user has actually clicked save. So I went about checking the flags - this did not help.

And so I did this very ugly piece of test code to see if I could "sample" the mouse key state.
giving my fingers enough time while incremting a dwell variable from 0 to 0xffff while checking GetAsyncKeyState(l_Button....

It never sees the mouse click.

Then it dawned on me, I am holding up this applcations message queue while I am waiting for
a mouse click. - that won't happen.

If that is the case, I am at a loss right now as how to detect a mouse click on &Save (Save menu item) instead of firing when I cruise over it with the mouse.

Here is the ugly buggered up section where I sample the mouse button.

C++
int dwell=0;
if(!strncmp(szmenu,"&Save",5)) // mouse is over Save menu item
{
	while(dwell < 0xffff) // long enough for me to click mouse - proof of concept....
        {
		if ((GetAsyncKeyState(VK_LBUTTON) & 0x80) != 0)
	        {
			dwell = 0xffff; // terminate while
			WritePersistFile = 1; // do what we do when user clicks Save
		}
		dwell++;
	}
}
Posted

I suspect that this way lies madness. I think you don't need to be sampling WM_MENUSELECT at all but either WM_COMMAND or whatever else is generated as a message by actually clicking on File Save.
 
Share this answer
 
Ok I'm Stupid, Mad and maybe an idiot, but I did it.

WM_MENUSELECT Click mouse detection for anyone sturggling with this as I was.

in the MenuHookProc, when I have found via WM_MENUSELECT that the mouse is hovering over Save,
I set a global bool variable to true other wise false.

C++
if(!strncmp(szmenu,"&Save",5)) // mouse is over Save menu item
{
      MouseOnSave = true;
}else{
     MouseOnSave = false;
}


Then I hook the mouse.

The WM_MENUSELECT coming into the MenuHookProc above arrives before any WM_LBUTTONDOWN message
arriving below.

In the mouseproc()

I look for WM_LBUTTONDOWN and if so, then check the state of the global MouseOnSave. If it's true, The we are on the Save button and have clicked it so fire off what we are to do in that
instance.

Works for me for now.
 
Share this answer
 
Comments
Matthew Faithfull 11-Mar-13 16:54pm    
Well done. Sometimes the hard way is the only way. As long as you're not wanting to support old Win9x Windows variants you should be fine ( Menu related messages can turn up in a different order ).
Ron Anders 12-Mar-13 9:54am    
Ya know it wasn't hard (took about 10 minutes) once the light went on. I was in that condition because I could not believe, and still can't that all we can do in a main menu hook is receive a bunch of WM_MENUSELECTS as we scan over the sub menus with no confirmation messages of any kind. I had to be missing something as well as many other poor souls in the net through the "ages" I might add. Some of the posts date back more than a decade. MS wouldn't give us all this and then drop us off here to walk the rest of the way home would they?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900