从另一个应用程序获取按钮句柄
我有一个程序需要将 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);
}
}
Spy 屏幕截图
I have a program that needs to send the BM_CLICK message to another applications button.
I can get the parent window handle but when I try to get the button handle if always returns 0
I got the button caption name and button type from Spy++ it seems right but I know I must have gotten something wrong. below is my code
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);
}
}
Spy screen shot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试为窗口文本传递 null,然后尝试查找任何按钮:
之后,您可以使用第二个参数和新的调用来多次获取下一个按钮句柄。
另外,您能否尝试检查
Marshal.GetLastWin32Error
以查看错误结果是什么?Try to pass null for the window text and instead try to find any button:
After that you can use the second parameter and a new call to get the next button handle a couple more times.
Also could you please try checking
Marshal.GetLastWin32Error
to see what the error result is?试试这个:
Try this:
你可以这样做:
这应该可以解决问题。
You can do somthing like this:
This should do the trick.