Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simulate mouse click anywhere

Posted on 1998-08-09
6
Medium Priority
?
1,371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

636 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