码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-05-06 00:59:39      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:代码   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);

        /// <summary>
        /// 根据父窗口的标题和子窗口的类名返回子窗口的句柄
        /// </summary>
        /// <param name="HwndName">子窗口的类名</param>
        /// <param name="WindowName">父窗口的标题</param>
        /// <returns></returns>
        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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!