Example #1
0
        /// <summary>
        /// Get the dimensions of the specified window. Includes location and size.
        /// </summary>
        /// <param name="hWnd">The handle to the window.</param>
        /// <returns>The dimensions of the window.</returns>
        public static Rectangle GetDimensions(IntPtr hWnd)
        {
            Structs.Rect hWndRect = new Structs.Rect();
            WinAPI.GetWindowRect(hWnd, out hWndRect);

            return(new Rectangle(hWndRect.X, hWndRect.Y, hWndRect.Width, hWndRect.Height));
        }
Example #2
0
        /// <summary>
        /// Convert a global coordinate to a window coordinate.
        /// </summary>
        /// <param name="hWnd">The handle to the window.</param>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <returns>The coordinate.</returns>
        public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y)
        {
            Structs.Rect hWndRect = new Structs.Rect();
            WinAPI.GetWindowRect(hWnd, out hWndRect);
            Point point = new Point(hWndRect.X + x, hWndRect.Y + y);

            return(point);
        }
Example #3
0
        /// <summary>
        /// Get the mouse position relative to the top left corner of a window.
        /// </summary>
        /// <param name="hWnd">The handle to the window.</param>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <returns>The mouse position.</returns>
        public static Point GetCoordinateRelativeToWindow(IntPtr hWnd)
        {
            Structs.Rect hWndRect = new Structs.Rect();
            WinAPI.GetWindowRect(hWnd, out hWndRect);
            int   x     = Cursor.Position.X;
            int   y     = Cursor.Position.Y;
            Point point = new Point(x - hWndRect.X, y - hWndRect.Y);

            return(point);
        }
Example #4
0
        /// <summary>
        /// Get a screenshot of the specified window.
        /// </summary>
        /// <param name="hWnd">The handle to the window.</param>
        /// <returns></returns>
        public static Bitmap Screenshot(IntPtr hWnd)
        {
            Normalize(hWnd);
            Structs.Rect rc;
            WinAPI.GetWindowRect(hWnd, out rc);

            Bitmap   bmp       = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
            Graphics gfxBmp    = Graphics.FromImage(bmp);
            IntPtr   hdcBitmap = gfxBmp.GetHdc();

            WinAPI.PrintWindow(hWnd, hdcBitmap, 0);

            gfxBmp.ReleaseHdc(hdcBitmap);
            gfxBmp.Dispose();

            // If you want to leave out the window title and borders use this:
            //bmp = ImageProcessing.Crop(bmp, new Rectangle(8, 30, bmp.Width - 16, bmp.Height - 30 - 8));

            return(bmp);
        }