Solved

Simulate mouse click anywhere

Posted on 1998-08-09
6
1,344 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

18 Experts available now in Live!

Get 1:1 Help Now