用 c 调用 win-api 实现自动点击c# winform 程序 的按钮

c# 程序界面

要点击的按钮是 “Start”

c程序代码: 

#include <windows.h>
void main()
{
    HWND windowHandle = FindWindowA(NULL, "WatchDog v1.04 (2010-06-22)");   //Can’t find a proccess

    if( windowHandle ) 
    {
        HWND hwndChild = FindWindowEx(windowHandle, NULL, NULL, NULL);

        while ( hwndChild )
        {
            HWND hwndChildChild = FindWindowEx(hwndChild, NULL, NULL, L"Start");
            if(hwndChildChild)
            {
                SendMessage(hwndChildChild, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0, 0));
                Sleep(100);
                SendMessage(hwndChildChild, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0, 0));
                break;
            }
            hwndChild = FindWindowEx(windowHandle, hwndChild, NULL, NULL);
        }
    }



    return;
}

功能简介: 

首先用 spy++ 确定 c# 程序的 caption 是 WatchDog v1.04 (2010-06-22) 

然后用spy++ 找出 button  所在的 hierachy position ,  如下图黑线包围的是一个panel , button 在panel 里面, 也就是说 ,  panel 是 mainform 的 child window , button 是 panel 的 child window ,所以代码中向下找2层

原文地址:https://www.cnblogs.com/lthxk-yl/p/7411620.html