Generating Windows Events

Started by
17 comments, last by imadoki 18 years, 1 month ago
Please, I need help as fast as possible... I will greatly appreciate any input on this matter. The thing is I am developing a small client server program and it works also with &#106avascript etc. Ok, so, I am using Win32 for one of the client apps and I need to automatically click one or more buttons at a specific location on the dialog box. Ok, I will restate it to reduce any confusion: I have a dialog box with a few buttons, like "accept", "decline" or "continue" and I want the program to automatically click on this button, i.e. I want the win32 app in C++ to generate the mouse click on a specific position (in this case, on the button, or on an edit box, to make it active), how do I do that? how do I generate the event of a mouse left button click at a specific x,y location in Win32 in C++ or C? Thanks.
Advertisement
SendMessage plus target window HWND plus WM_COMMAND plus target control ID.

You can also fuss with WM_LBUTTONDOWN, WM_LBUTTONUP and mouse coordinates, but that will probably entail a lot more work than it's worth.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
To simulate a left click at a given position(screen coords):

SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

Hope that helps.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Instead of mouse_event, use SendInput
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
The easiest way to do this Imo is to use SendMessage like LessBread said, and send a "BM_CLICK" message.

HWND pTargetWindowHandle = FindWindowEx(FindWindow("Parent", NULL), NULL, "Child", NULL);SendMessage(pTargetWindowHandle, BM_CLICK, NULL, NULL);


Check for errors in WindowHandle if you want.
Quote:Original post by Extrarius
Instead of mouse_event, use SendInput


Isn't mouse_event much easier to use though? I had a look at the SendInput function and mouse_event just seems alot simpler. Granted SendInput may be useful for other things that are more involved. But given the simplicity of what the OP wants to do, I'd say mouse_event does the job in a strightforward manner.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Listen to MSDN! -> "Windows NT/2000/XP: This function has been superseded. Use SendInput instead." :)
Quote:Original post by raz0r
Listen to MSDN! -> "Windows NT/2000/XP: This function has been superseded. Use SendInput instead." :)


Cleverly hidden at the top of the page [lol]
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
If it is your app that is creating the dialog in the first place then just call the event handler for the button yourself directly. If it is your own set of apps that you could send the other app a custom message instructing it to call the event handler manually. In either case you don't have to futz with the pure evil that is simulating user input via SendInput / mouse_event / etc.

(sincerely hoping you're not trying to automatically click buttons on a security dialog on behalf of the actual user...)
-Mike
Thank you all. No, no, absolutely not. It is just that I am working on a quick fix till I get around to a new version of the client app. The old app was made by someone else and is hell to work on its source code, so until a new version can talk to the updated server, this is there as a stop gap method.

I will try all of the advised stuff later this evening. Thanks.

This topic is closed to new replies.

Advertisement