I am trying to generate an click event in a third party application. As a start I tried to simulate a click in calculator. Here's the code"
IntPtr hwnd = IntPtr.Zero;
IntPtr hwndChild = IntPtr.Zero;
//Get a handle for the Calculator Application main window
hwnd = FindWindow(null, "Calculator");
hwndChild = FindWindowEx(hwnd, IntPtr.Zero, "Button", "1");
//send BN_CLICKED message
SendMessage(hwndChild, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
But using this code I am not getting the handle of the button. Could someone help please. Is there any other way to simulate a button click on third party application?
Thanks.
It started to work when I replaced
with
and used
Your general approach is correct, but there are two potential problems with your code:
FindWindowEx
only finds direct children of the specified window. It's possible that the calculator buttons are laid out in a container window which is a child of the main window, so the button wouldn't be a direct child of the main window.The documentation for
BM_CLICK
says that it simulates a click by sending mouse down and up messages and hence you may have to activate the parent window before sending this message.First use SPY++ to find that is a button having Handle.
In some cases the controls that looks like Button can be graphic control. The difference is the graphic control will not have Handle, but the Windows control will have handle. If that control has valid Handle.Then use FindWindowEx
Also give Parent window Handle (I think first parameter, May be you have to use GetWindow() using the caption)
Then send click message.
If you haven't the handle of the button, you can emulate mouse clicking on coordinates:
But, function SetCursorPos set cursor position in global coordinates of screen, so fist you shoud get the possition of a third party application's window.