Link to home
Start Free TrialLog in
Avatar of helpmealot
helpmealot

asked on

Simulate mouse click anywhere

Hi,

I was wondering, is there some API call, or MFC member function, that would let me simulate a left/right mouse click ANYWHERE on the screen?

I know about ::SendMessage (HWND_BROADCAST, yaddi yoddi ya... but that doesn't seem to work, because the message isn't sent to child windows, and the coordinates are relative to my program's main window, which is a dialog box.

Here is the code I am currently using to try to get this to work:

LPARAM temp;
WORD lo, hi;
ScreenToClient (AfxGetApp ()->m_pMainWnd->m_hWnd, &command.m_Point);
lo = (WORD) command.m_Point.x;
hi = (WORD) command.m_Point.y;
temp = (LPARAM) (((LPARAM) (lo)) | ((LPARAM) (hi << 16)));

::SendMessage (HWND_BROADCAST, WM_LBUTTONDOWN, MK_LBUTTON, temp);

The command.m_Point is a POINT stucture containing the screen coordinates where I want the mouse click to be simulated.  I have tried the code with and without the ScreenToClient function, no effect.  I also made sure that the casting was being done correctly as you can see.  I imagine I can take out a lot of it, but I was just experimenting.  I have also tried, instead of HWND_BROADCAST, AfxGetApp ()->m_pMainWnd, still no effect.

Anyone have any insight as to how to accomplish this?

Thanks,
Help Me A Lot! :-)
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Use WindowFromPoint() and then ChildWindowFromPoint() to find the child window that is under the desired point.  Then send a WM_LBUTTONDOWN message followed by a WM_LBUTTONUP message to this child window.

Ask if you have questions.
Note the coordiantes sent in the message must be realtive to the client area of the target child area.  The coordiantes you are probably working with are screen coordinates, if so, you will want to use ScreenToClient() to convert the screen coordinates to coordinates relative to the target child window's client area.  
Avatar of helpmealot

ASKER

Thank you nietod.  While waiting for an answer, I stumbled upon one of my own.  The function mouse_event does exactly what I want, well almost.  Do you have any thoughts as to which method I should use?  The mouse_event is nice, but I have to scale the coordinates up to its weird scheme (0 - 65535) which is a pain.

Thanks.
I was not familiar with mouse_event().  There is a corresponding function for the keyboard called Keybd_event() that I am familiar with.  In the case of faking keyboard events, it is much easier and more reliable to use Keybd_event() rathenr than sending "faked" key-down and keu-up messages.  That would suggest that mouse_event() will be easier and more reliable than faking mouse messages.  I would try it and see.  However, you may have a problem.  I'm afraid that mouse_event might actually move the mouse pointer on the screen.  I don't know for sure, if this is true, however, you'll have to try and see.
I experimented with it and have gotten it to work exactly the way I want.  In the documentation it says "The mouse_event function synthesizes mouse motion and button clicks."  It doesn't necessarily have to change the position of the mouse, which is good, because I was using SetCursorPos to move the mouse :-)

Anyway, thanks for your help.