从另一个应用程序获取Button句柄

Mik*_*ike 7 c# winapi sendmessage

我有一个程序需要将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);

    }

}
Run Code Online (Sandbox Code Playgroud)

间谍屏幕截图

替代文字

Bri*_*ndy 5

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

IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", null);
Run Code Online (Sandbox Code Playgroud)

之后,您可以使用第二个参数和一个新调用来使下一个按钮处理多次.

你还可以试试看Marshal.GetLastWin32Error看错误结果是什么吗?