public static extern bool AdjustWindowRectEx(ref RECT lpRect, WS style, bool bMenu, WS_EX exStyle);
 public static extern bool GetWindowRect(HWND hwnd, ref RECT lpRect);
 public static extern bool GetClientRect(HWND hwnd, ref RECT lpRect);
        /// <summary>
        ///     Returns a bitmap of the contents of the window.
        /// </summary>
        /// <remarks>
        ///     RedirectedWindow maintains a bitmap internally and only
        ///     allocates a new bitmap if the dimensions of the window
        ///     have changed.  Even if UpdateRedirectedBitmap returns the same
        ///     bitmap, the contents are guaranteed to have been updated with
        ///     the current contents of the window.
        /// </remarks>
        public BitmapSource UpdateRedirectedBitmap()
        {
            RECT rcClient = new RECT();
            NativeMethods.GetClientRect(Handle, ref rcClient);
            if (_interopBitmap == null || rcClient.width != _bitmapWidth || rcClient.height != _bitmapHeight) {
                if (_interopBitmap != null) {
                    DestroyBitmap();
                }

                CreateBitmap(rcClient.width, rcClient.height);
            }

            // PrintWindow doesn't seem to work any better than BitBlt.
            // TODO: make it an option
            // User32.NativeMethods.PrintWindow(Handle, _hDC, PW.DEFAULT);

            IntPtr hdcSrc = NativeMethods.GetDC(Handle);
            NativeMethods.BitBlt(_hDC, 0, 0, _bitmapWidth, _bitmapHeight, hdcSrc, 0, 0, ROP.SRCCOPY);
            NativeMethods.ReleaseDC(Handle, hdcSrc);

            if (_interopBitmap != null) {
                _interopBitmap.Invalidate();
            }

            return _interopBitmap;
        }
        /// <summary>
        ///     Sizes the window such that the client area has the specified
        ///     size.
        /// </summary>
        public bool SetClientAreaSize(int width, int height)
        {
            POINT ptClient = new POINT();
            NativeMethods.ClientToScreen(Handle, ref ptClient);

            RECT rect = new RECT();
            rect.left = ptClient.x;
            rect.top = ptClient.y;
            rect.right = rect.left + width;
            rect.bottom = rect.top + height;

            WS style = (WS)NativeMethods.GetWindowLong(Handle, GWL.STYLE);
            WS_EX exStyle = (WS_EX)NativeMethods.GetWindowLong(Handle, GWL.EXSTYLE);
            NativeMethods.AdjustWindowRectEx(ref rect, style, false, exStyle);

            NativeMethods.SetWindowPos(
                Handle,
                HWND.NULL,
                rect.left,
                rect.top,
                rect.width,
                rect.height,
                SWP.NOZORDER | SWP.NOCOPYBITS);

            NativeMethods.GetClientRect(Handle, ref rect);
            return rect.width == width && rect.height == height;
        }
        /// <summary>
        ///     Aligns the RedirectedWindow such that the specified client
        ///     coordinate is aligned with the specified screen coordinate.
        /// </summary>
        public void AlignClientAndScreen(int xClient, int yClient, int xScreen, int yScreen)
        {
            POINT pt = new POINT(xClient, yClient);
            NativeMethods.ClientToScreen(Handle, ref pt);

            int dx = xScreen - pt.x;
            int dy = yScreen - pt.y;

            RECT rcWindow = new RECT();
            NativeMethods.GetWindowRect(Handle, ref rcWindow);

            NativeMethods.SetWindowPos(
                Handle,
                HWND.NULL,
                rcWindow.left + dx,
                rcWindow.top + dy,
                0,
                0,
                SWP.NOSIZE | SWP.NOZORDER | SWP.NOACTIVATE);
        }