Exemplo n.º 1
0
        static public System.Drawing.Bitmap GetScreenshotOfWindowAsBitmap(IntPtr windowHandle)
        {
            SetProcessDPIAware();

            var windowRect = new WinApi.Rect();

            if (WinApi.GetWindowRect(windowHandle, ref windowRect) == IntPtr.Zero)
            {
                return(null);
            }

            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            var asBitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            //  Where from could we get the scale to apply to compute the rect in screen coordinates?
            Console.WriteLine("System.Drawing.Graphics.FromImage(asBitmap).DpiX: " + System.Drawing.Graphics.FromImage(asBitmap).DpiX);

            Console.WriteLine("System.Drawing.Graphics.FromHwnd(GetDesktopWindow()).DpiX: " + System.Drawing.Graphics.FromHwnd(WinApi.GetDesktopWindow()).DpiX);
            Console.WriteLine("System.Drawing.Graphics.FromHwnd(windowHandle).DpiX: " + System.Drawing.Graphics.FromHwnd(windowHandle).DpiX);

            System.Drawing.Graphics.FromImage(asBitmap).CopyFromScreen(
                windowRect.left,
                windowRect.top,
                0,
                0,
                new System.Drawing.Size(width, height),
                System.Drawing.CopyPixelOperation.SourceCopy);

            return(asBitmap);
        }
Exemplo n.º 2
0
        static void performTaskOnWindow(Request.TaskOnIdentifiedWindowStructure taskOnIdentifiedWindow)
        {
            var windowHandle = new IntPtr(taskOnIdentifiedWindow.windowId.WindowHandleFromInt);

            var windowRect = new WinApi.Rect();

            if (WinApi.GetWindowRect(windowHandle, ref windowRect) == IntPtr.Zero)
            {
                return;
            }

            var inputSimulator = new WindowsInput.InputSimulator();

            var task = taskOnIdentifiedWindow.task;

            if (task.BringWindowToForeground != null)
            {
                WinApi.SetForegroundWindow(windowHandle);
                WinApi.ShowWindow(windowHandle, WinApi.SW_RESTORE);
            }

            if (task.MoveMouseToLocation != null)
            {
                WinApi.SetCursorPos(
                    task.MoveMouseToLocation.x + windowRect.left,
                    task.MoveMouseToLocation.y + windowRect.top);
            }

            if (task.MouseButtonDown != null)
            {
                if (task.MouseButtonDown.MouseButtonLeft != null)
                {
                    inputSimulator.Mouse.LeftButtonDown();
                }

                if (task.MouseButtonDown.MouseButtonRight != null)
                {
                    inputSimulator.Mouse.RightButtonDown();
                }
            }

            if (task.MouseButtonUp != null)
            {
                if (task.MouseButtonUp.MouseButtonLeft != null)
                {
                    inputSimulator.Mouse.LeftButtonUp();
                }

                if (task.MouseButtonUp.MouseButtonRight != null)
                {
                    inputSimulator.Mouse.RightButtonUp();
                }
            }

            if (task.KeyboardKeyDown != null)
            {
                inputSimulator.Keyboard.KeyDown((WindowsInput.Native.VirtualKeyCode)task.KeyboardKeyDown.KeyboardKeyFromVirtualKeyCode);
            }

            if (task.KeyboardKeyUp != null)
            {
                inputSimulator.Keyboard.KeyUp((WindowsInput.Native.VirtualKeyCode)task.KeyboardKeyUp.KeyboardKeyFromVirtualKeyCode);
            }
        }