Click a button in another application

I want to use SendMessageor PostMessageto click a button in another application

I have sample code to do this by getting a Window Handle, but it doesn't work

I also used "WinDowse" to get the required information. here is the code

private const uint BM_CLICK = 0x00F5;
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;

private void PushOKButton(IntPtr ptrWindow)
{
    WindowHandle = FindWindow(null, "Form1"); 
    if (ptrWindow == IntPtr.Zero)
      return;

    IntPtr ptrOKButton = FindWindowEx(ptrWindow, IntPtr.Zero, "Button", "&Yes");

    if (ptrOKButton == IntPtr.Zero)
      return;

    SendMessage(ptrOKButton, WM_LBUTTONDOWN, 0, 0);
    SendMessage(ptrOKButton, WM_LBUTTONUP, 0, 0);
    SendMessage(ptrOKButton, BM_CLICK, 0, 0);
}

Is there a Compelete Suloution in C #?

+5
source share
2 answers

You have the right general idea. There are always tricks for automation.

Down / Up button - brings you close to the raw action of pressing, but not quite the same. You will want to consider

  • coordinates you click on - some buttons don’t like boundary clicks at 0,0
  • mouse position - the mouse outside the button at the start or end may lead to its inoperability.
  • - , , , 10

BM_Click - win32, - , , .

, WM_GetText , , .

, #.

, AutoIT, .

0

win32 API - , / .

POINT p;
BOOL cursorPosGetSuccessful = GetCursorPos(&p);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, horA, verA, NULL, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, NULL, 0);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, horB, verB, NULL, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, NULL, 0);
if (cursorPosGetSuccessful)// put the mouse back to roughly where it used to be before the scan.  
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, p.x, p.y, NULL, 0);
-1

All Articles