Example #1
0
        public static void RestoreForegroundWindowAndMouse()
        {
            var forefroundWindow = NativeMethods.GetForegroundWindow();

            for (var num = 0; forefroundWindow != _originalForegroundWindow && num < 1000; num++)
            {
                if (!NativeMethods.SetForegroundWindow(_originalForegroundWindow))
                {
                    break;
                }
                Thread.Sleep(1);
                forefroundWindow = NativeMethods.GetForegroundWindow();
            }

            var structInput = new NativeMethods.Input {
                type = NativeMethods.SendInputEventType.InputMouse
            };
            double fScreenWidth  = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CXSCREEN) - 1;
            double fScreenHeight = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CYSCREEN) - 1;

            structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.ABSOLUTE | NativeMethods.MouseEventFlags.MOVE;
            structInput.mkhi.mi.dx      = (int)(_originalMousePos.X * (65535.0f / fScreenWidth));
            structInput.mkhi.mi.dy      = (int)(_originalMousePos.Y * (65535.0f / fScreenHeight));
            NativeMethods.SendInput(1, ref structInput, SizeOfInput);
        }
Example #2
0
        private static bool ClickAtPos(
            IntPtr hWnd, int x, int y, bool left, bool doubleClick = false, bool restore = true,
            Func <bool> restoreCondition = null, CharacterProfile profile = null)
        {
            var    wndBounds     = GetWindowRect(hWnd);
            double fScreenWidth  = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CXSCREEN) - 1;
            double fScreenHeight = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CYSCREEN) - 1;
            var    fx            = (wndBounds.Left + x) * (65535.0f / fScreenWidth);
            var    fy            = (wndBounds.Top + y) * (65535.0f / fScreenHeight);

            var structInput = new NativeMethods.Input {
                type = NativeMethods.SendInputEventType.InputMouse
            };

            structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.ABSOLUTE |
                                          NativeMethods.MouseEventFlags.MOVE;

            structInput.mkhi.mi.dx = (int)fx;
            structInput.mkhi.mi.dy = (int)fy;

            var forefroundWindow = NativeMethods.GetForegroundWindow();

            if (restore)
            {
                SaveForegroundWindowAndMouse();
            }

            if (!BringWindowIntoFocus(hWnd, profile))
            {
                return(false);
            }

            try
            {
                NativeMethods.BlockInput(true);
                // check one last time if window is still in foreground after we block input.
                if (NativeMethods.GetForegroundWindow() != hWnd)
                {
                    return(false);
                }

                NativeMethods.SendInput(1, ref structInput, SizeOfInput);
                Thread.Sleep(80);

                if (left)
                {
                    structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.LEFTDOWN |
                                                  NativeMethods.MouseEventFlags.LEFTUP;
                }
                else
                {
                    structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.RIGHTDOWN |
                                                  NativeMethods.MouseEventFlags.RIGHTUP;
                }

                NativeMethods.SendInput(1, ref structInput, SizeOfInput);
                SleepForMouseInputReaction();
                if (doubleClick)
                {
                    NativeMethods.SendInput(1, ref structInput, SizeOfInput);
                    SleepForMouseInputReaction();
                }
            }
            finally
            {
                if (restore)
                {
                    try
                    {
                        if (restoreCondition != null)
                        {
                            while (!restoreCondition())
                            {
                                Thread.Sleep(1);
                            }
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                        RestoreForegroundWindowAndMouse();
                    }
                    catch
                    {
                    }
                }
                NativeMethods.BlockInput(false);
            }
            return(true);
        }
Example #3
0
        public static void LeftClickAtPos(
            IntPtr hWnd, int x, int y, bool doubleClick = false, bool restore = true, Func <bool> restoreCondition = null)
        {
            var    wndBounds     = GetWindowRect(hWnd);
            double fScreenWidth  = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CXSCREEN) - 1;
            double fScreenHeight = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CYSCREEN) - 1;
            double fx            = (wndBounds.Left + x) * (65535.0f / fScreenWidth);
            double fy            = (wndBounds.Top + y) * (65535.0f / fScreenHeight);

            var structInput = new NativeMethods.Input {
                type = NativeMethods.SendInputEventType.InputMouse
            };

            structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.ABSOLUTE | NativeMethods.MouseEventFlags.MOVE | NativeMethods.MouseEventFlags.LEFTDOWN |
                                          NativeMethods.MouseEventFlags.LEFTUP;
            structInput.mkhi.mi.dx = (int)fx;
            structInput.mkhi.mi.dy = (int)fy;

            var forefroundWindow = NativeMethods.GetForegroundWindow();

            if (restore)
            {
                SaveForegroundWindowAndMouse();
            }
            try
            {
                NativeMethods.BlockInput(true);

                for (int num = 0; forefroundWindow != hWnd && num < 1000; num++)
                {
                    NativeMethods.SetForegroundWindow(hWnd);
                    Thread.Sleep(1);
                    forefroundWindow = NativeMethods.GetForegroundWindow();
                }

                NativeMethods.SendInput(1, ref structInput, SizeOfInput);
                if (doubleClick)
                {
                    Thread.Sleep(100);
                    NativeMethods.SendInput(1, ref structInput, SizeOfInput);
                }
            }
            finally
            {
                if (restore)
                {
                    try
                    {
                        if (restoreCondition != null)
                        {
                            while (!restoreCondition())
                            {
                                Thread.Sleep(1);
                            }
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                        RestoreForegroundWindowAndMouse();
                    }
                    catch { }
                }
                NativeMethods.BlockInput(false);
            }
        }