Пример #1
0
        /// <summary>
        /// 查找窗体上控件句柄
        /// </summary>
        /// <param name="hwnd">父窗体句柄</param>
        /// <param name="lpszWindow">控件标题(Text)</param>
        /// <param name="bChild">设定是否在子窗体中查找</param>
        /// <returns>控件句柄,没找到返回IntPtr.Zero</returns>
        private static IntPtr FindWindowEx(IntPtr hwnd, string lpszWindow, bool bChild)
        {
            IntPtr iResult = IntPtr.Zero;

            // 首先在父窗体上查找控件
            iResult = BaseWin32Api.FindWindowEx(hwnd, 0, null, lpszWindow);
            // 如果找到直接返回控件句柄
            if (iResult != IntPtr.Zero)
            {
                return(iResult);
            }

            // 如果设定了不在子窗体中查找

            if (!bChild)
            {
                return(iResult);
            }

            var list = EnumChildWindows(hwnd);

            //var list2 = new List<IntPtr>();
            //foreach (var item in list)
            //{
            //    list2 = EnumChildWindows(item);
            //}
            //list.AddRange(list2);

            // 枚举子窗体,查找控件句柄
            //int i = BaseWin32Api.EnumChildWindows(
            //hwnd,
            //(h, l) =>
            //{
            //    var tool = Utility.GetAutomationElementFromHandle(h);
            //    var uiElement = Utility.FindAutoElementByPath(h, new string[] { lpszWindow });
            //    IntPtr f1 = BaseWin32Api.FindWindowEx(h, 0, null, lpszWindow);
            //    if (f1 == IntPtr.Zero)
            //        return true;
            //    else
            //    {
            //        iResult = f1;
            //        return false;
            //    }
            //},
            //0);
            // 返回查找结果
            return(iResult);
        }