Solved

Simulate mouse click anywhere

Posted on 1998-08-09
6
1,340 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now