Solved

Simulate mouse click anywhere

Posted on 1998-08-09
6
1,350 Views
Last Modified: 2012-08-13
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! :-)
0
Comment
Question by:helpmealot
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1169789
Answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169790
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169791
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.  
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:helpmealot
ID: 1169792
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169793
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.
0
 

Author Comment

by:helpmealot
ID: 1169794
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question