/// <summary> /// /// </summary> /// <param name="hwndSource"></param> /// <returns></returns> public static Rect GetWindowRect(this HwndSource hwndSource) { HWND hwnd = new HWND(hwndSource.Handle); RECT rcWindow = new RECT(); NativeMethods.GetWindowRect(hwnd, ref rcWindow); // Transform from pixels into DIPs. Point position = new Point(rcWindow.left, rcWindow.top); Vector size = new Vector(rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top); position = hwndSource.CompositionTarget.TransformFromDevice.Transform(position); size = hwndSource.CompositionTarget.TransformFromDevice.Transform(size); return new Rect(position, size); }
public static Size GetClientSize(this HwndSource hwndSource) { HWND hwnd = new HWND(hwndSource.Handle); RECT rcClient = new RECT(); NativeMethods.GetClientRect(hwnd, ref rcClient); // Client rect should always have (0,0) as the upper-left corner. Debug.Assert(rcClient.left == 0); Debug.Assert(rcClient.left == 0); // Convert from pixels into DIPs. Vector size = new Vector(rcClient.right, rcClient.bottom); size = hwndSource.CompositionTarget.TransformFromDevice.Transform(size); return new Size(size.X, size.Y); }
/// <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); }
/// <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; }
public bool FillRect(ref RECT rect, COLOR color) { return FillRect(this, ref rect, (int)color + 1); }
private static extern bool FillRect(HDC hDC, ref RECT lprc, int color);
public bool FillRect(ref RECT rect, HBRUSH brush) { return FillRect(this, ref rect, brush); }
private static extern bool FillRect(HDC hDC, ref RECT lprc, HBRUSH hbr);
public static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, DT uFormat);
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);