Sign in to follow this  

Generating Windows Events

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 javascript 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ok, the stuff works great, I just get the HWND, then get its position, then add the no. of pixles to the button and viola!

Now, I need to bug you guys some more. I want this stop gap solution to be a little more elegent, so the HWND of the child window that contains the buttons will be of the dims 10x10 so that nothing in it can be seen. Now, how do I simulate a mouse click inside this window?

Ok, I will state it in another way, just incase the 1st was too loopy...

So, I have a dialog box, that has a child "container" which contains the buttons. For the sake of making it look clean, I will make this "container" window (hwnd) of the dim 1x1 or 10x10 and then would like to simulate a mouse button click on a button contained in this HWND. So, as you can guess, I have the HWND pointer at my disposal. Please help.

You guys gave me an amazing work around the instant I asked. I would be extremely grateful if you could present ideas and solutions for this aswell.

Thanks.

Share this post


Link to post
Share on other sites
You cannot correctly simulate a mouse event to something that could not be interacted with normally.

You can partially simulate the mouse event using SendMessage, which is probably good enough for your situation

Share this post


Link to post
Share on other sites
I can use SendMessage to post WM_LBUTTONDOWN, but it isn't working, I guess the mouse pos. Is it lParam or wParam and how do you send the pos using SendMessage?

I used google, but I still can't find the answer to this, please pleeeeeease help.

EDIT: FYI, I cannot get the hwnd handles for the individual buttons, hence...

Share this post


Link to post
Share on other sites
Quote:
Original post by imadoki
I can use SendMessage to post WM_LBUTTONDOWN, but it isn't working, I guess the mouse pos. Is it lParam or wParam and how do you send the pos using SendMessage?

Quote:
From WM_LBUTTONDOWN
wParam
Indicates whether various virtual keys are down. This parameter can be one or more of the following values.
MK_CONTROL
The CTRL key is down.
MK_LBUTTON
The left mouse button is down.
MK_MBUTTON
The middle mouse button is down.
MK_RBUTTON
The right mouse button is down.
MK_SHIFT
The SHIFT key is down.
MK_XBUTTON1
Windows 2000/XP: The first X button is down.
MK_XBUTTON2
Windows 2000/XP: The second X button is down.

lParam
The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

In other words:
SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(x_coord, y_coord));

Share this post


Link to post
Share on other sites
Hi,

I tried that a few minutes after I posted my last post. I found the info on google, but guess what? it doesn't work. :(

Would you have any other solution to this?

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike2343
Works for me. Sure you're sending it to the right Window?[...]
I know for certain he is not:
Quote:
Original post by imadoki
[...]EDIT: FYI, I cannot get the hwnd handles for the individual buttons, hence...
Is there any reason you can't get the window handles for the individual buttons?

Share this post


Link to post
Share on other sites
Sounds to me like he's trying to "hack" a program. It'd be a shame for you to do all of this work just to find out that the developer had already thought that someone would be stupid enough to try exactly that :)

You know, those trial-period programs where some of the feature buttons have been disabled? I think he's trying to programmatically click those :P

..That or he's trying to write a macro for an online game -.-

Share this post


Link to post
Share on other sites
Really guys, I am not trying to hack, except my company's old code!

The thing is, the old system was a spaghetti system with an old Win32 program with some sort of own scripting program (in house made) that displays buttons etc.

The thing is we are going more for the elegent solution as we will be using Java. But till we can roll it out, we were doing this. Anyway, we are probably going to be concurrently running 2 sever versions instead of 1 for now.

Thanks anyway.

Ya, and it still isn't working. :-(

Share this post


Link to post
Share on other sites

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this