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

前端之家收集整理的这篇文章主要介绍了c# – 从另一个应用程序获取Button句柄前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个程序需要将BM_CLICK消息发送到另一个应用程序按钮.
我可以获得父窗口句柄,但是当我尝试获取按钮句柄时,如果总是返回0

我从间谍那里得到了按钮标题名称和按钮类型似乎没错,但我知道我一定是弄错了.下面是我的代码

  1. public const Int BM_CLICK = 0x00F5;
  2.  
  3. [DllImport("user32.dll",CharSet = CharSet.Auto,SetLastError = true)]
  4. private static extern IntPtr SendMessage(IntPtr hwnd,uint Msg,IntPtr wParam,IntPtr lParam);
  5.  
  6. [DllImport("user32.dll",SetLastError = true)]
  7. static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);
  8.  
  9.  
  10.  
  11. private void button1_Click(object sender,EventArgs e)
  12. {
  13. Process[] processes = Process.GetProcessesByName("QSXer");
  14.  
  15. foreach (Process p in processes)
  16. {
  17. ////the Button's Caption is "Send" and it is a "Button".
  18. IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle,IntPtr.Zero,"Button","Send");
  19. //ButtonHandle is always zero thats where I think the problem is
  20. SendMessage(ButtonHandle,BM_CLICK,IntPtr.Zero);
  21.  
  22. }
  23.  
  24. }

间谍屏幕截图

解决方法

尝试为窗口文本传递null,而是尝试查找任何按钮:
  1. IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle,null);

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

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

猜你在找的C#相关文章