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.
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.
Find the handle to the button that you want to click (by using FindWindowEx), and just send click message:
SendMessage(hButton, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0, 0));
SendMessage(hButton, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0, 0));
EnumChildWindows, until you find out target button handle. If WinAPI contained a function that would directly "click" on of the window's buttons, it would do exactly the same that we did. Also, WinAPI is treating buttons as (child) windows.BM_CLICK message instead of two WM_LBUTTON... messages: SendMessage(hButton, BM_CLICK, 0, 0);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.
WinUser.h, BN_CLICKED is defined as follows: #define BN_CLICKED 0, so the MAKEWPARAM is superfluous here. WPARAM(idOfButton) is all you need for a WM_COMMAND that resulted from a mouse click, unless you are trying to document in the code.SendMessage(WM_COMMAND, MAKEWPARAM(IDC_BUTTON_MODIFY_TALK, BN_CLICKED), (LPARAM)m_btnModify.GetSafeHwnd());Try this for OK:
SendMessage(hWnd, WM_COMMAND, 1, NULL);
Here is a complete function:
void clickControl(HWND hWnd, int x, int y)
{
POINT p;
p.x = x; p.y = y;
ClientToScreen(hWnd, &p);
SetCursorPos(p.x, p.y);
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));
}