SetWindowPos 可以改变窗体位置
引用:
/// <summary>
/// 该函数改变一个窗口的大小、坐标、垂直顺序
/// </summary>
/// <param name="hWnd">目标窗口句柄</param>
/// <param name="hWndInsertAfter">枚举值(垂直顺序)</param>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <param name="Width">宽</param>
/// <param name="Height">高</param>
/// <param name="flags">枚举</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);
hWndInsertAfter 枚举值:
/// <summary>
/// hWndInsertAfter 枚举
/// </summary>
public enum WindowsZOrder
{
HWND_BOTTOM = 1,
HWND_NOTOPMOST = -2,
HWND_TOP = 0,
HWND_TOPMOST = -1
}
Windows flags 枚举:
public enum WindowsFlags
{
//flags 参数可选值:
SWP_ASYNCWINDOWPOS = 0x4000, //若调用进程不拥有窗口, 系统会向拥有窗口的线程发出需求
SWP_DEFERERASE = 0x2000, //防止产生 WM_SYNCPAINT 消息
SWP_DRAWFRAME = 0x0020, //画边框
SWP_FRAMECHANGED = 0x0020, //强制发送 WM_NCCALCSIZE 消息, 一般只是在改变大小时才发送此消息
SWP_HIDEWINDOW = 0x0080, //隐藏窗口
SWP_NOACTIVATE = 0x0010, //不激活
SWP_NOCOPYBITS = 0x0100, //丢弃客户区
SWP_NOMOVE = 0x0002, //忽略 X、Y, 不改变位置
SWP_NOOWNERZORDER = 0x0200, //忽略 hWndInsertAfter, 不改变 Z 序列的所有者
SWP_NOREDRAW = 0x0008, //不重绘
SWP_NOREPOSITION = 0x0200, //忽略 hWndInsertAfter, 不改变 Z 序列的所有者
SWP_NOSENDCHANGING = 0x0400, //不发出 WM_WINDOWPOSCHANGING 消息
SWP_NOSIZE = 0x0001, //忽略 cx、cy, 保持大小
SWP_NOZORDER = 0x0004, //忽略 hWndInsertAfter, 保持 Z 顺序
SWP_SHOWWINDOW = 0x0040 //显示窗口
}
调用时,可先获取当前屏幕工作区域,已方便设置宽和高,例如:
Screen currentScreen = Screen.FromHandle((IntPtr)); //多屏时可以获取当前屏幕
System.Drawing.Rectangle currentRectangle = currentScreen.WorkingArea;
int widht = currentRectangle.Width;
int height = crrentRectangle.Height;