加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:15:51 所属栏目:百科 来源:网络整理
导读:我有一个程序需要将BM_CLICK消息发送到另一个应用程序按钮. 我可以获得父窗口句柄,但是当我尝试获取按钮句柄时,如果总是返回0 我从间谍那里得到了按钮标题名称和按钮类型似乎没错,但我知道我一定是弄错了.下面是我的代码 public const Int BM_CLICK = 0x00F5
我有一个程序需要将BM_CLICK消息发送到另一个应用程序按钮.
我可以获得父窗口句柄,但是当我尝试获取按钮句柄时,如果总是返回0

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

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",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);

    }

}

间谍屏幕截图

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读