C# 根据父窗口的标题和子窗口的类名返回子窗口的句柄

2021-03-07 10:26

阅读:411

标签:代码   his   stat   cli   句柄   dna   href   项目   htm   

原文:C# 根据父窗口的标题和子窗口的类名返回子窗口的句柄

很老的项目中的代码,现在也想不起来为什么要这么写了。

技术图片
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public extern static int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        [DllImport("User32.dll ")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
        public delegate bool CallBack(IntPtr hwnd, int lParam);

        /// 
        /// 根据父窗口的标题和子窗口的类名返回子窗口的句柄
        /// 
        /// 子窗口的类名
        /// 父窗口的标题
        /// 
        public static IntPtr FindWindowExOrde(string HwndName, string WindowName)
        {
            IntPtr HwndId = new IntPtr();

            IntPtr hwnd = FindWindow(null, WindowName);

            IntPtr ChildHwndWindow = FindWindowEx(hwnd, IntPtr.Zero, null, null);

            var H = EnumChildWindows(hwnd, (x, l) =>
            {

                StringBuilder sb = new StringBuilder(HwndName.Length + 2);

                if (x != IntPtr.Zero)
                {
                    GetClassName(x, sb, sb.Capacity);

                    if (HwndName.Equals(sb.ToString()))
                    {
                        HwndId = x;
                        return true;
                    }
                }
                else
                    return false;

                return true;
            }, 0);


            return HwndId;



        }
技术图片

 

C# 根据父窗口的标题和子窗口的类名返回子窗口的句柄

标签:代码   his   stat   cli   句柄   dna   href   项目   htm   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12833897.html


评论


亲,登录后才可以留言!