示例#1
0
 public static extern IntPtr GetClientRect(IntPtr hWnd, ref NativeStructs.RECT rect);
示例#2
0
        /// <summary>
        /// Requests the specified window to draw itself to a Bitmap
        /// </summary>
        /// <param name="handle">The handle of the window to draw</param>
        /// <param name="clientArea">If the client area should be drawn or the whole window.</param>
        /// <remarks>This method only works on windows that handle WM_PRINT. It allows for capturing non-visible and minimized applications.</remarks>
        /// <returns>An image with the window drawn onto it.</returns>
        public static Bitmap Print(IntPtr hWnd, bool clientArea)
        {
            NativeStructs.RECT r = new NativeStructs.RECT();
            NativeMethods.GetWindowRect(hWnd, ref r);
            Rectangle rect = r;
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                IntPtr hdc = g.GetHdc();
                NativeMethods.PrintWindow(hWnd, hdc, 0);
                g.ReleaseHdc(hdc);
            }

            if (clientArea)
            {
                NativeMethods.GetClientRect(hWnd, ref r);
                Point pt = Point.Empty.ClientToScreen(hWnd);
                Rectangle client = new Rectangle(pt, ((Rectangle)r).Size);

                Bitmap crop = new Bitmap(client.Width, client.Height);
                using (Graphics g = Graphics.FromImage(crop))
                {
                    int titleHeight = client.Top - rect.Top;
                    g.DrawImage(bmp, new Point((client.Width - rect.Width) / 2, -titleHeight));
                }
                bmp.Dispose();
                bmp = crop;
            }

            return bmp;
        }
示例#3
0
 public static extern IntPtr GetWindowRect(IntPtr hWnd, ref NativeStructs.RECT rect);