Exemplo n.º 1
0
        /// <summary>
        /// 全局搜索桌面UI元素
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public static List <AutomationElement> FindGlobalElementByName(string names)
        {
            var list = new List <AutomationElement>();

            BaseWin32Api.EnumWindows((hwnd, l) =>
            {
                //if ((list?.Count ?? 0) > 0)
                //    return false;
                //var rootUI = Utility.GetAutomationElementFromHandle(hwnd);
                //if ((rootUI?.Current.Name ?? "").Contains("思迅"))
                //{

                //}
                int i = BaseWin32Api.EnumChildWindows(hwnd, (h, l2) =>
                {
                    var uiElement = Utility.FindAutoElementByPath(h, new string[] { names });
                    if (uiElement != null)
                    {
                        System.Console.WriteLine(hwnd.ToString() + "id->" + uiElement.Current.AutomationId);
                        //TextHelper.Write($"json->{JsonConvert.SerializeObject(uiElement.Current)}");
                        list.Add(uiElement);
                        return(false);
                    }
                    System.Console.WriteLine("true");
                    return(true);
                }, 0);

                return(true);
            }, 0);
            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 全局搜索桌面UI元素
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public static List <AutomationElement> FindGlobalElementByName(string names)
        {
            var    list       = new List <AutomationElement>();
            IntPtr desktopPtr = BaseWin32Api.GetDesktopWindow();

            //2、获得一个子窗口(这通常是一个顶层窗口,当前活动的窗口)
            IntPtr winPtr = BaseWin32Api.GetWindow(desktopPtr, BaseWin32Api.GetWindowCmd.GW_CHILD);

            //3、循环取得桌面下的所有子窗口
            while (winPtr != IntPtr.Zero)
            {
                //4、继续获取下一个子窗口
                winPtr = BaseWin32Api.GetWindow(winPtr, BaseWin32Api.GetWindowCmd.GW_HWNDNEXT);

                var rootElement = Utility.GetAutomationElementFromHandle(winPtr);

                var uiElement = Utility.FindAutoElementListByPath(winPtr, null);
                if (uiElement != null)
                {
                    foreach (var ui in uiElement)
                    {
                        TextHelper.Write("查找内容===>" + ui.Current.Name + "---rootElement->" + rootElement.Current.Name);
                        if (ui.Current.Name.Contains(names))
                        {
                            list.Add(ui);
                        }
                    }
                }

                TextHelper.Write("查找内容===>总共查到" + (uiElement?.Count ?? 0) + "个子元素 ---rootElement->" + rootElement?.Current.Name ?? "");
            }
            return(list);
        }
Exemplo n.º 3
0
        public static void DrawRectangle(int x, int y, int width, int height, int lineWidth, Color color)
        {
            IntPtr desk   = BaseWin32Api.GetDesktopWindow();
            IntPtr deskDC = BaseWin32Api.GetDCEx(desk, IntPtr.Zero, 0x403);

            BaseWin32Api.RedrawWindow(IntPtr.Zero, null, IntPtr.Zero, 0x85);
            using (Graphics g = Graphics.FromHdc(deskDC))
            {
                //实例化Pen类
                System.Drawing.Pen myPen = new System.Drawing.Pen(color, lineWidth);
                g.DrawRectangle(myPen, 0, 0, width - lineWidth, height - lineWidth);
            }
            //g.DrawString("Lightning", new Font("宋体", 50, FontStyle.Bold), Brushes.Red, new PointF(100, 100));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public static void GetList()
        {
            var    list   = new List <IntPtr>();
            IntPtr winPtr = BaseWin32Api.GetDesktopWindow();

            var list2 = EnumChildWindows(winPtr, "51.50");

            ////2、获得一个子窗口(这通常是一个顶层窗口,当前活动的窗口)
            //IntPtr winPtr = BaseWin32Api.GetWindow(desktopPtr, BaseWin32Api.GetWindowCmd.GW_CHILD);

            //3、循环取得桌面下的所有子窗口
            while (winPtr != IntPtr.Zero)
            {
                //4、继续获取下一个子窗口
                winPtr = BaseWin32Api.GetWindow(winPtr, BaseWin32Api.GetWindowCmd.GW_HWNDNEXT);

                var tool = Utility.GetAutomationElementFromHandle(winPtr);
                if (tool == null)
                {
                    continue;
                }

                if (tool != null)
                {
                    TextHelper.Write("==========" + tool.Current.Name + "-" + tool.Current.LocalizedControlType);
                    System.Console.WriteLine(tool.Current.Name + "-" + tool.Current.LocalizedControlType);
                }

                if (!tool.Current.Name.Contains("思迅天店"))
                {
                    continue;
                }


                var toolElement = FindWindowEx(winPtr, "909.90", true);

                //var uiElement = Utility.FindAutoElementByPath(winPtr, new string[] { "2019108.00" });
                //if (uiElement != null)
                //{
                //    System.Console.WriteLine("找到金额" + uiElement.Current.Name);
                //    TextHelper.Write("=====找到金额=====" + uiElement.Current.Name);
                //}
            }

            System.Console.WriteLine("便利结束");
        }
Exemplo n.º 6
0
        public void MouseMoveEventHandler(object sender, System.Windows.Forms.MouseEventArgs data)
        {
            this.txbCode.Text = $"x:{data.X},y:{data.Y},clickCount:{data.Clicks},button:{data.Button.ToString()}";

            #region 查找坐标句柄并且跟上次对比
            //根据当前鼠标位置获得窗口句柄
            var currentHwnd = BaseWin32Api.WindowFromPoint();
            if (maindHwnd == currentHwnd)
            {
                return;
            }

            maindHwnd = currentHwnd;
            if (currentHwnd == null)
            {
                return;
            }
            #endregion

            #region 查找句柄的当前元素
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //获得当前窗口句柄的UI元素
            var rootHwnd = Utility.GetAutomationElementFromHandle(currentHwnd);
            stopwatch.Stop();
            this.txbCode.Text += $"查找跟UI 花费时间:{stopwatch.ElapsedMilliseconds}毫秒";
            if (rootHwnd != null)
            {
                if (rootHwnd.Current.Name.Equals(this.txbMoney.Text))
                {
                    stopwatch.Start();
                    GDIWin32Api.DrawRectangle(currentHwnd, Convert.ToInt32(rootHwnd.Current.BoundingRectangle.Width), Convert.ToInt32(rootHwnd.Current.BoundingRectangle.Height), 2, System.Drawing.Color.Red);
                    stopwatch.Stop();
                    this.txbCode.Text       += $"绘图 花费时间:{stopwatch.ElapsedMilliseconds}毫秒";
                    this.txbDescription.Text = $"总共找到{1}处金额,{rootHwnd.Current.Name}";
                }
            }
            #endregion

            TextHelper.WriteAsync(this.txbCode.Text);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 根据窗口句柄矩形
        /// </summary>
        /// <param name="tarent">目标窗口句柄</param>
        /// <param name="width">矩形宽度</param>
        /// <param name="height">矩形高度</param>
        /// <param name="lineWidth">矩形线条宽度(粗细度)</param>
        /// <param name="color">颜色</param>
        public static void DrawRectangle(IntPtr tarent, int width, int height, int lineWidth, Color color)
        {
            if (tarent == null || width <= 0 || height <= 0 || lineWidth <= 0)
            {
                return;
            }
            Task.Factory.StartNew(() =>
            {
                //获得句柄的DC
                var hdc = BaseWin32Api.GetWindowDC(tarent);
                //if (LastPtr != null)
                //    BaseWin32Api.RedrawWindow(hdc, null, IntPtr.Zero, 0x85);

                //TODO 重新绘图会导致卡顿问题[性能受到很大的影响]
                BaseWin32Api.RedrawWindow(IntPtr.Zero, null, IntPtr.Zero, 0x85);

                Thread.Sleep(400);
                //if (graphics == null)
                //    graphics = Graphics.FromHdc(hdc);
                //else
                //    graphics = Graphics.FromHdc(hdc);

                ////实例化Pen类
                //if (_pen == null)
                //    _pen = new System.Drawing.Pen(color, lineWidth);

                ////调用Graphics对象的DrawRectangle方法
                //graphics.DrawRectangle(_pen, 0, 0, width - lineWidth, height - lineWidth);

                using (Graphics gr = Graphics.FromHdc(hdc))
                {
                    //实例化Pen类
                    var pen = new System.Drawing.Pen(color, lineWidth);

                    //调用Graphics对象的DrawRectangle方法
                    gr.DrawRectangle(pen, 0, 0, width - lineWidth, height - lineWidth);
                }
                LastPtr = tarent;
            });
        }