Example #1
0
        /// <summary>
        ///     Takes a bitmap screenshot of a particular window
        /// </summary>
        /// <param name="processName">The process/window's title</param>
        /// <returns>Bitmap of the window</returns>
        public static Bitmap getScreenshotOfWindow(string windowTitle)
        {
            //This will hold our window's information
            var WINDOW = new NativeWin32.RECT();

            //Get the window information
            IntPtr window = getWindowHandle(windowTitle);

            //Get window details
            GetWindowRect(window, out WINDOW);

            //Width and height of window
            int winWidth  = WINDOW.Right - WINDOW.Left;
            int winHeight = WINDOW.Bottom - WINDOW.Top;

            //Window size
            var winSize = new Size(winWidth, winHeight);

            //Our graphics variables
            var      screenshot = new Bitmap(winWidth, winHeight);
            Graphics graphic    = Graphics.FromImage(screenshot);

            //Takes the screenshot, starting where the top left corner of the window is, and takes only the size of the window
            graphic.CopyFromScreen(WINDOW.Left, WINDOW.Top, 0, 0, winSize, CopyPixelOperation.SourceCopy);

            //Clean up a bit
            graphic.Dispose();

            //Return the screenshot
            return(screenshot);
        }
Example #2
0
        /// <summary>
        ///     Drags the mouse relative to a window
        /// </summary>
        /// <param name="windowHandle">The window handle you want to move relative to</param>
        /// <param name="start">Drag start position</param>
        /// <param name="end">Drag end position</param>
        /// <param name="human">Human mouse movement or not</param>
        /// <param name="steps">Number of steps to take</param>
        /// <returns>True or false on outcome</returns>
        public static bool dragMouseRelative(IntPtr windowHandle, Point start, Point end, bool human = true, int steps = 100)
        {
            //Will sore window details
            var WINDOW = new NativeWin32.RECT();

            //Get window details
            if (!GetWindowRect(windowHandle, out WINDOW))
            {
                return(false);
            }

            //Move mouse to start point
            moveMouse(new Point(WINDOW.Left + start.X, WINDOW.Top + start.Y), human, steps);
            //Left button down
            leftDown();
            //Drag mouse to end point
            moveMouse(new Point(WINDOW.Left + end.X, WINDOW.Top + end.Y), human, steps);
            // Left button up
            leftUp();

            // if mouse position is right
            if (Cursor.Position == end)
            {
                //Return true
                return(true);
            }
            return(false);
        }
Example #3
0
        /// <summary>
        ///     Moves the mouse relative to the window. eg if you specified for it to move  to 10,10 over the window "Notepad"
        ///     it would move 10,10 into the top left of Notepad. A bit like local scope where 0,0 is the top left corner of the
        ///     window given.
        /// </summary>
        /// <param name="windowHandle">Window handle</param>
        /// <param name="targetX">Trget X position</param>
        /// <param name="targetY">Target Y position</param>
        /// <param name="human">Human or not mouse movement</param>
        /// <param name="steps">Number of steps to take</param>
        /// <returns>True or false depending on outcome</returns>
        public static bool moveMouseRelative(IntPtr windowHandle, Point target, bool human = true, int steps = 100)
        {
            //Will sore window details
            var WINDOW = new NativeWin32.RECT();

            //Get window details
            if (!GetWindowRect(windowHandle, out WINDOW))
            {
                return(false);
            }

            //Move the mouse
            if (!moveMouse(new Point(WINDOW.Left + target.X, WINDOW.Top + target.Y), human, steps))
            {
                return(false);
            }

            return(true);
        }
Example #4
0
 private static extern bool GetWindowRect(IntPtr hWnd, out NativeWin32.RECT lpRect);