private static class Win32
public static class User32
/// 改变一个子窗口、弹出式窗口和顶层窗口的尺寸、位置和 Z 序。
/// <param name="hWnd">窗口句柄。</param>
/// <param name="hWndInsertAfter">
/// 在z序中的位于被置位的窗口前的窗口句柄。该参数必须为一个窗口句柄,或下列值之一:
/// <para>HWND_BOTTOM:将窗口置于 Z 序的底部。如果参数hWnd标识了一个顶层窗口,则窗口失去顶级位置,并且被置在其他窗口的底部。</para>
/// <para>HWND_NOTOPMOST:将窗口置于所有非顶层窗口之上(即在所有顶层窗口之后)。如果窗口已经是非顶层窗口则该标志不起作用。</para>
/// <para>HWND_TOP:将窗口置于Z序的顶部。</para>
/// <para>HWND_TOPMOST:将窗口置于所有非顶层窗口之上。即使窗口未被激活窗口也将保持顶级位置。</para>
/// <param name="x">以客户坐标指定窗口新位置的左边界。</param>
/// <param name="y">以客户坐标指定窗口新位置的顶边界。</param>
/// <param name="cx">以像素指定窗口的新的宽度。</param>
/// <param name="cy">以像素指定窗口的新的高度。</param>
/// <param name="wFlagslong">
/// 窗口尺寸和定位的标志。该参数可以是下列值的组合:
/// <para>SWP_ASYNCWINDOWPOS:如果调用进程不拥有窗口,系统会向拥有窗口的线程发出需求。这就防止调用线程在其他线程处理需求的时候发生死锁。</para>
/// <para>SWP_DEFERERASE:防止产生 WM_SYNCPAINT 消息。</para>
/// <para>SWP_DRAWFRAME:在窗口周围画一个边框(定义在窗口类描述中)。</para>
/// <para>SWP_FRAMECHANGED:给窗口发送 WM_NCCALCSIZE 消息,即使窗口尺寸没有改变也会发送该消息。如果未指定这个标志,只有在改变了窗口尺寸时才发送 WM_NCCALCSIZE。</para>
/// <para>SWP_HIDEWINDOW:隐藏窗口。</para>
/// <para>SWP_NOACTIVATE:不激活窗口。如果未设置标志,则窗口被激活,并被设置到其他最高级窗口或非最高级组的顶部(根据参数hWndlnsertAfter设置)。</para>
/// <para>SWP_NOCOPYBITS:清除客户区的所有内容。如果未设置该标志,客户区的有效内容被保存并且在窗口尺寸更新和重定位后拷贝回客户区。</para>
/// <para>SWP_NOMOVE:维持当前位置(忽略X和Y参数)。</para>
/// <para>SWP_NOOWNERZORDER:不改变 Z 序中的所有者窗口的位置。</para>
/// <para>SWP_NOREDRAW:不重画改变的内容。如果设置了这个标志,则不发生任何重画动作。适用于客户区和非客户区(包括标题栏和滚动条)和任何由于窗回移动而露出的父窗口的所有部分。如果设置了这个标志,应用程序必须明确地使窗口无效并区重画窗口的任何部分和父窗口需要重画的部分。</para>
/// <para>SWP_NOREPOSITION:与 SWP_NOOWNERZORDER 标志相同。</para>
/// <para>SWP_NOSENDCHANGING:防止窗口接收 WM_WINDOWPOSCHANGING 消息。</para>
/// <para>SWP_NOSIZE:维持当前尺寸(忽略 cx 和 cy 参数)。</para>
/// <para>SWP_NOZORDER:维持当前 Z 序(忽略 hWndlnsertAfter 参数)。</para>
/// <para>SWP_SHOWWINDOW:显示窗口。</para>
/// <returns>如果函数成功,返回值为非零;如果函数失败,返回值为零。若想获得更多错误消息,请调用 GetLastError 函数。</returns>
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
public static extern Int32 SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, Int32 x, Int32 y, Int32 cx,
Int32 cy, Int32 wFlagslong);
public enum WindowPositionFlags
/// If the calling thread and the thread that owns the window are attached to different input queues, the system posts
/// the request to the thread that owns the window. This prevents the calling thread from blocking its execution while
/// other threads process the request.
SWP_ASYNCWINDOWPOS = 0x4000,
/// Prevents generation of the WM_SYNCPAINT message.
/// Draws a frame (defined in the window's class description) around the window.
/// Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if
/// the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's
/// size is being changed.
SWP_FRAMECHANGED = 0x0020,
/// Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the
/// topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
/// Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client
/// area are saved and copied back into the client area after the window is sized or repositioned.
/// Retains the current position (ignores X and Y parameters).
/// Does not change the owner window's position in the Z order.
SWP_NOOWNERZORDER = 0x0200,
/// Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area,
/// the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a
/// result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any
/// parts of the window and parent window that need redrawing.
/// Same as the SWP_NOOWNERZORDER flag.
SWP_NOREPOSITION = 0x0200,
/// Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSENDCHANGING = 0x0400,
/// Retains the current size (ignores the cx and cy parameters).
/// Retains the current Z order (ignores the hWndInsertAfter parameter).
public const string LibraryName = "user32";
/// <param name="hWnd"></param>
/// <param name="lpRect"></param>
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport(LibraryName, ExactSpelling = true)]
public static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
[StructLayout(LayoutKind.Sequential)]
public partial struct Rectangle : IEquatable<Rectangle>
/// <param name="left"></param>
/// <param name="top"></param>
/// <param name="right"></param>
/// <param name="bottom"></param>
public Rectangle(int left = 0, int top = 0, int right = 0, int bottom = 0)
/// <param name="width">矩形的宽度</param>
/// <param name="height">矩形的高度</param>
public Rectangle(int width = 0, int height = 0) : this(0, 0, width, height)
public bool Equals(Rectangle other)
return (Left == other.Left) && (Right == other.Right) && (Top == other.Top) &&
(Bottom == other.Bottom);
public override bool Equals(object obj)
return obj is Rectangle && Equals((Rectangle) obj);
public static bool operator ==(Rectangle left, Rectangle right)
return left.Equals(right);
public static bool operator !=(Rectangle left, Rectangle right)
public override int GetHashCode()
var hashCode = (int) Left;
hashCode = (hashCode * 397) ^ (int) Top;
hashCode = (hashCode * 397) ^ (int) Right;
hashCode = (hashCode * 397) ^ (int) Bottom;
public bool IsEmpty => this.Left == 0 && this.Top == 0 && this.Right == 0 && this.Bottom == 0;
get { return unchecked((int) (Right - Left)); }
set { Right = unchecked((int) (Left + value)); }
get { return unchecked((int) (Bottom - Top)); }
set { Bottom = unchecked((int) (Top + value)); }
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public static Rectangle Create(int x, int y, int width, int height)
return new Rectangle(x, y, (int) (width + x), (int) (height + y));
public static Rectangle From(ref Rectangle lvalue, ref Rectangle rvalue,
Func<int, int, int> leftTopOperation,
Func<int, int, int> rightBottomOperation = null)
if (rightBottomOperation == null)
rightBottomOperation = leftTopOperation;
leftTopOperation(lvalue.Left, rvalue.Left),
leftTopOperation(lvalue.Top, rvalue.Top),
rightBottomOperation(lvalue.Right, rvalue.Right),
rightBottomOperation(lvalue.Bottom, rvalue.Bottom)
public void Add(Rectangle value)
Add(ref this, ref value);
public void Subtract(Rectangle value)
Subtract(ref this, ref value);
public void Multiply(Rectangle value)
Multiply(ref this, ref value);
public void Divide(Rectangle value)
Divide(ref this, ref value);
public void Deflate(Rectangle value)
Deflate(ref this, ref value);
public void Inflate(Rectangle value)
Inflate(ref this, ref value);
public void Offset(int x, int y)
public void OffsetTo(int x, int y)
OffsetTo(ref this, x, y);
public void Scale(int x, int y)
public void ScaleTo(int x, int y)
public static void Add(ref Rectangle lvalue, ref Rectangle rvalue)
lvalue.Left += rvalue.Left;
lvalue.Top += rvalue.Top;
lvalue.Right += rvalue.Right;
lvalue.Bottom += rvalue.Bottom;
public static void Subtract(ref Rectangle lvalue, ref Rectangle rvalue)
lvalue.Left -= rvalue.Left;
lvalue.Top -= rvalue.Top;
lvalue.Right -= rvalue.Right;
lvalue.Bottom -= rvalue.Bottom;
public static void Multiply(ref Rectangle lvalue, ref Rectangle rvalue)
lvalue.Left *= rvalue.Left;
lvalue.Top *= rvalue.Top;
lvalue.Right *= rvalue.Right;
lvalue.Bottom *= rvalue.Bottom;
public static void Divide(ref Rectangle lvalue, ref Rectangle rvalue)
lvalue.Left /= rvalue.Left;
lvalue.Top /= rvalue.Top;
lvalue.Right /= rvalue.Right;
lvalue.Bottom /= rvalue.Bottom;
public static void Deflate(ref Rectangle target, ref Rectangle deflation)
target.Top += deflation.Top;
target.Left += deflation.Left;
target.Bottom -= deflation.Bottom;
target.Right -= deflation.Right;
public static void Inflate(ref Rectangle target, ref Rectangle inflation)
target.Top -= inflation.Top;
target.Left -= inflation.Left;
target.Bottom += inflation.Bottom;
target.Right += inflation.Right;
public static void Offset(ref Rectangle target, int x, int y)
public static void OffsetTo(ref Rectangle target, int x, int y)
var width = target.Width;
var height = target.Height;
public static void Scale(ref Rectangle target, int x, int y)
public static void ScaleTo(ref Rectangle target, int x, int y)
x = (int) (target.Left / x);
y = (int) (target.Top / y);
[DllImport(LibraryName, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out Point lpPoint);
[StructLayout(LayoutKind.Sequential)]
public Point(int x, int y)
public static implicit operator System.Drawing.Point(Point p)
return new System.Drawing.Point(p.X, p.Y);
public static implicit operator Point(System.Drawing.Point p)
return new Point(p.X, p.Y);