我有一个需要向其他应用程序的按钮发送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);
}
}
间谍屏幕截图