在C#中,SetWindowPos函数用于设置窗口的位置和大小。
原型:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
参数说明:
-
hWnd
: 要设置位置和大小的窗口的句柄(handle)。 -
hWndInsertAfter
: 确定窗口的 Z 顺序,即窗口如何在 Z 顺序中放置。通常可以使用以下值之一:IntPtr.Zero
: 将窗口放在 Z 顺序的顶部。new IntPtr(-1)
: 将窗口放在所有非顶层窗口的顶部。new IntPtr(-2)
: 将窗口放在所有窗口的顶部。- 其他窗口句柄:将窗口放在指定窗口的顶部。
-
X
和Y
: 窗口的新位置的左上角的 X 和 Y 坐标。 -
cx
和cy
: 窗口的新宽度和高度。 -
uFlags
: 设置窗口位置和大小的标志。可以使用以下标志的组合来控制窗口的行为:-
SWP_NOSIZE
: 维持当前尺寸(忽略 cx 和 cy 参数)。 -
SWP_NOMOVE
: 维持当前位置(忽略 X 和 Y 参数)。 -
SWP_NOZORDER
: 维持当前 Z 顺序(忽略 hWndInsertAfter 参数)。 -
SWP_SHOWWINDOW
: 如果窗口在调用时是隐藏的,显示窗口。 -
SWP_HIDEWINDOW
: 隐藏窗口。 -
SWP_NOACTIVATE
: 不激活窗口。仅适用于SWP_SHOWWINDOW
标志。 -
SWP_DRAWFRAME
: 在调整窗口大小时,重绘窗口边框(通常与SWP_FRAMECHANGED
一起使用)。 -
SWP_NOOWNERZORDER
: 不改变拥有者窗口的 Z 顺序。 -
SWP_NOSENDCHANGING
: 防止窗口接收WM_WINDOWPOSCHANGING
消息。 -
SWP_FRAMECHANGED
: 使系统发送WM_NCCALCSIZE
消息,即使窗口的尺寸没有改变。 -
SWP_NOCOPYBITS
: 防止窗口重绘。 -
SWP_NOREPOSITION
: 不重新定位窗口,即使大小或位置发生变化。
-
示例用法:
1、改变窗口大小:
using System;
using System.Runtime.InteropServices;
// 定义常量和方法签名
public class Win32
{
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public const uint SWP_SHOWWINDOW = 0x40;
}
// 获取窗口句柄并调用 SetWindowPos 函数改变窗口大小
IntPtr hWnd = // 窗口句柄
int newX = // 新的 X 坐标
int newY = // 新的 Y 坐标
int newWidth = // 新的宽度
int newHeight = // 新的高度
Win32.SetWindowPos(hWnd, Win32.HWND_TOPMOST, newX, newY, newWidth, newHeight, Win32.SWP_SHOWWINDOW);
其中窗口句柄可以使用FindWindow函数获取窗口句柄(后面同),如:
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr hWnd = Win32.FindWindow(null, "窗口标题"); // 替换为窗口的类名或标题
2、移动到屏幕的左上角:
using System;
using System.Runtime.InteropServices;
// 定义常量和方法签名
public class Win32
{
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}
// 在代码中调用 SetWindowPos 函数将窗口移动到屏幕左上角
IntPtr hWnd = // 窗口句柄
Win32.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);
3、使其成为Topmost窗口并移动到屏幕的左上角:
using System;
using System.Runtime.InteropServices;
// 定义常量和方法签名
public class Win32
{
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_SHOWWINDOW = 0x0040;
public const int HWND_TOPMOST = -1;
public const int HWND_NOTOPMOST = -2;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}
// 在代码中调用 SetWindowPos 函数将窗口移动到屏幕左上角并设置为Topmost
IntPtr hWnd = // 窗口句柄
Win32.SetWindowPos(hWnd, Win32.HWND_TOPMOST, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);
4、显示窗口:
using System;
using System.Runtime.InteropServices;
// 定义常量和方法签名
public class Win32
{
public const int SW_SHOWNORMAL = 1;
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
// 获取窗口句柄并调用 ShowWindow 函数显示窗口
IntPtr hWnd = // 窗口句柄
Win32.ShowWindow(hWnd, Win32.SW_SHOWNORMAL);
5、隐藏窗口:
using System;
using System.Runtime.InteropServices;
// 定义常量和方法签名
public class Win32
{
public const int SW_HIDE = 0;
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
// 获取窗口句柄并调用 ShowWindow 函数隐藏窗口
IntPtr hWnd = // 窗口句柄
Win32.ShowWindow(hWnd, Win32.SW_HIDE);