How can I simulate a button click by specifying a window handle to a button?

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

I have a handle to this window. This is the Abort / Retry / Ignore window.

I do not want to go with a click simulation with the X and Y coordinates, as this does not fit my needs.

+10
source share
5 answers

Find the handle of the button you want to click (using FindWindowEx ), and simply send a message with a click:

 SendMessage(hButton, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0, 0)); SendMessage(hButton, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0, 0)); 
+9
source

Send a BM_CLICK message to the HWND buttons:

 SendMessage(hButton, BM_CLICK, 0, 0); 

This forces the button to receive WM_LBUTTONDOWN and WM_LBUTTONUP messages, and the parent to receive the BN_CLICKED notification, as if the user physically clicked on the button.

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

As a rule, you can do without hwndOfButton , if you do not know it, it depends on the implementation of the dialog!

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

+7
source

Try this for OK:

 SendMessage(hWnd, WM_COMMAND, 1, NULL); 
+3
source

Here is the full function:

 void clickControl(HWND hWnd, int x, int y) { POINT p; px = x; py = y; ClientToScreen(hWnd, &p); SetCursorPos(px, py); PostMessage(hWnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y)); PostMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y)); PostMessage(hWnd, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x, y)); } 
+1
source

All Articles