Beispiel #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);
        }
Beispiel #2
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;
            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 |
                                          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 (var num = 0; forefroundWindow != hWnd && num < 1000; num++)
                {
                    NativeMethods.SetForegroundWindow(hWnd);
                    Thread.Sleep(10);
                    forefroundWindow = NativeMethods.GetForegroundWindow();
                }

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