Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simulate a button click given the handle to the button's window?

Tags:

c++

button

winapi

I want to simulate a click on a button located in a dialog box.

I have the handle to that window. This is an Abort/Retry/Ignore kind of window.

I don't want to go with simulating a click having X and Y coordinates as it doesn't suit my needs.

like image 653
AB. Avatar asked Apr 30 '13 08:04

AB.


2 Answers

SendMessage(hParent, WM_COMMAND, MAKEWPARAM(IdOfButton, BN_CLICKED), (LPARAM)hwndOfButton);

Typically you can get away without the hwndOfButton, if you don't know it - depends on the dialog's implementation!

It can be SendMessage or PostMessage, depending on your use case.

like image 145
noelicus Avatar answered Sep 17 '22 12:09

noelicus


Send a BM_CLICK message to the HWND of the button:

SendMessage(hButton, BM_CLICK, 0, 0);

That causes the button to receive WM_LBUTTONDOWN and WM_LBUTTONUP messages, and the parent to receive an BN_CLICKED notification, as if the user had physically clicked on the button.

like image 25
Remy Lebeau Avatar answered Sep 19 '22 12:09

Remy Lebeau