Beispiel #1
0
        public bool ClickImage(ImageBuffer imageNeedle, double secondsToWait = 0, SearchRegion searchRegion = null, Point2D offset = default(Point2D), ClickOrigin origin = ClickOrigin.Center, MouseButtons mouseButtons = MouseButtons.Left)
        {
            if (origin == ClickOrigin.Center)
            {
                offset.x += imageNeedle.Width / 2;
                offset.y += imageNeedle.Height / 2;
            }

            if (searchRegion == null)
            {
                searchRegion = GetScreenRegion();
            }

            Vector2 matchPosition;
            double  bestMatch;

            if (searchRegion.Image.FindLeastSquaresMatch(imageNeedle, out matchPosition, out bestMatch, MatchLimit))
            {
                int screenHeight   = inputSystem.GetCurrentScreenHeight();
                int clickY         = (int)(searchRegion.ScreenRect.Bottom + matchPosition.Y + offset.y);
                int clickYOnScreen = screenHeight - clickY;                 // invert to put it on the screen

                Point2D screenPosition = new Point2D((int)matchPosition.X + offset.x, clickYOnScreen);
                SetMouseCursorPosition(screenPosition.x, screenPosition.y);

                inputSystem.CreateMouseEvent(GetMouseDown(mouseButtons), screenPosition.x, screenPosition.y, 0, 0);
                Delay(UpDelaySeconds);
                inputSystem.CreateMouseEvent(GetMouseUp(mouseButtons), screenPosition.x, screenPosition.y, 0, 0);

                return(true);
            }

            return(false);
        }
        public bool ClickImage(ImageBuffer imageNeedle, double secondsToWait = 0, SearchRegion searchRegion = null, Point2D offset = default(Point2D), ClickOrigin origin = ClickOrigin.Center, MouseButtons mouseButtons = MouseButtons.Left)
        {
            if (origin == ClickOrigin.Center)
            {
                offset.x += imageNeedle.Width / 2;
                offset.y += imageNeedle.Height / 2;
            }

            if (searchRegion == null)
            {
                searchRegion = GetScreenRegion();
            }

            Vector2 matchPosition;
            double  bestMatch;

            if (searchRegion.Image.FindLeastSquaresMatch(imageNeedle, out matchPosition, out bestMatch, MatchLimit))
            {
                int screenHeight   = inputSystem.GetCurrentScreenHeight();
                int clickY         = (int)(searchRegion.ScreenRect.Bottom + matchPosition.y + offset.y);
                int clickYOnScreen = screenHeight - clickY;                 // invert to put it on the screen

                Point2D screenPosition = new Point2D((int)matchPosition.x + offset.x, clickYOnScreen);
                SetMouseCursorPosition(screenPosition.x, screenPosition.y);
                switch (mouseButtons)
                {
                case MouseButtons.None:
                    break;

                case MouseButtons.Left:
                    inputSystem.CreateMouseEvent(NativeMethods.MOUSEEVENTF_LEFTDOWN, screenPosition.x, screenPosition.y, 0, 0);
                    Wait(UpDelaySeconds);
                    inputSystem.CreateMouseEvent(NativeMethods.MOUSEEVENTF_LEFTUP, screenPosition.x, screenPosition.y, 0, 0);
                    break;

                case MouseButtons.Right:
                    inputSystem.CreateMouseEvent(NativeMethods.MOUSEEVENTF_RIGHTDOWN, screenPosition.x, screenPosition.y, 0, 0);
                    Wait(UpDelaySeconds);
                    inputSystem.CreateMouseEvent(NativeMethods.MOUSEEVENTF_RIGHTUP, screenPosition.x, screenPosition.y, 0, 0);
                    break;

                case MouseButtons.Middle:
                    inputSystem.CreateMouseEvent(NativeMethods.MOUSEEVENTF_MIDDLEDOWN, screenPosition.x, screenPosition.y, 0, 0);
                    Wait(UpDelaySeconds);
                    inputSystem.CreateMouseEvent(NativeMethods.MOUSEEVENTF_MIDDLEUP, screenPosition.x, screenPosition.y, 0, 0);
                    break;

                default:
                    break;
                }

                return(true);
            }

            return(false);
        }