马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C# 调用外部程序 程序又打开了一个窗口 怎么取又打开那个窗口的句柄??

[复制链接]
查看4291 | 回复2 | 2017-6-18 14:26:18 | 显示全部楼层 |阅读模式
C#  调用外部程序 程序又打开了一个窗口 怎么取又打开那个窗口的句柄??
ibcadmin | 2017-6-19 11:42:35 | 显示全部楼层
根据进程名字取句柄
Empty风 | 2017-6-19 14:14:36 | 显示全部楼层
本帖最后由 Empty风 于 2017-6-19 14:17 编辑
[C#] 纯文本查看 复制代码
  [DllImport("user32.dll")]
        static extern IntPtr GetTopWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern Int32 GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);

        [DllImport("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        private static readonly UInt32 GW_HWNDNEXT = 2;
//运行方法 返回 PID
   public Int32 Run()
        {
            Process myProcess = new Process();
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "运行的程序";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
            myProcess.WaitForExit(2000);

            return myProcess.Id;
        }
//获取当前打开窗口的句柄ID
 public IntPtr GetWnd(Int32 pID, String className, String text)
        {
            pid = pID;
            IntPtr h = GetTopWindow(IntPtr.Zero);
            while (h != IntPtr.Zero)
            {
                UInt32 newID;
                GetWindowThreadProcessId(h, out newID);
                if (newID == pID)
                {
                    StringBuilder sbClassName = new StringBuilder(200);
                    StringBuilder sbText = new StringBuilder(200);

                    GetClassName(h, sbClassName, 200);
                    GetWindowText(h, sbText, 200);
                    if (sbClassName.ToString().IndexOf(className, StringComparison.CurrentCultureIgnoreCase) >= 0 &&
                        sbText.ToString().IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0)
                    {
                        break;
                    }
                }

                h = GetWindow(h, GW_HWNDNEXT);
            }

            return h;
        }
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则