7

我有一个程序需要将 BM_CLICK 消息发送到另一个应用程序按钮。我可以获取父窗口句柄,但是当我尝试获取按钮句柄时,如果总是返回 0

我从 Spy++ 获得了按钮标题名称和按钮类型,这似乎是对的,但我知道我一定是搞错了。下面是我的代码

 public const Int BM_CLICK = 0x00F5;

 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);



private void button1_Click(object sender, EventArgs e)
{
    Process[] processes = Process.GetProcessesByName("QSXer");

    foreach (Process p in processes)
    {
        ////the Button's Caption is "Send" and it is a "Button".  
        IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", "Send");
       //ButtonHandle is always zero thats where I think the problem is 
    SendMessage(ButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

    }

}

间谍屏幕截图

替代文字

4

4 回答 4

5

尝试为窗口文本传递 null 并尝试查找任何按钮:

IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", null);

之后,您可以使用第二个参数和一个新调用来多次获取下一个按钮句柄。

您也可以尝试检查Marshal.GetLastWin32Error一下错误结果是什么吗?

于 2010-09-23T01:17:31.180 回答
2

试试这个:

IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, null, "Send");
于 2012-12-25T07:48:35.127 回答
2

你可以这样做:

//Program finds a window and looks for button in window and clicks it

HWND buttonHandle = 0;

BOOL CALLBACK GetButtonHandle(HWND handle, LPARAM)
{
    char label[100];
    int size = GetWindowTextA(handle, label, sizeof(label));
    if (strcmp(label, "Send") == 0) // your button name
    {
        buttonHandle = handle;
        cout << "button id is: " << handle << endl;
        return false;
    }
    return true;
}

int main()

{
    HWND windowHandle = FindWindowA(NULL, "**Your Window Name**");

    if (windowHandle == NULL)
    {
        cout << "app isn't open." << endl;
    }

    else
    {
        cout << "app is open :) " << endl;
        cout << "ID is: " << windowHandle << endl;
        SetForegroundWindow(windowHandle);
        BOOL ret = EnumChildWindows(windowHandle, GetButtonHandle, 0); //find the button
        cout << buttonHandle << endl;
        if (buttonHandle != 0)
        {
            LRESULT res = SendMessage(buttonHandle, BM_CLICK, 0, 0);
        }
    }
}

这应该可以解决问题。

于 2016-12-14T10:04:15.743 回答
0

尝试将项目构建为 x86。我尝试并成功了!

于 2014-11-21T03:36:13.760 回答