方法一 【优势:添加方便,通用 ; 缺点 边框有点大,大概有5像素,看着有点不舒服】
//定义窗体风格常量 private const Int32 WS_THICKFRAME = 0x40000; private const Int32 WS_BORDER = 0x800000; protected override CreateParams CreateParams { get { CreateParams p = base.CreateParams; p.Style |= WS_THICKFRAME; //是窗体能够改变大小 p.Style &= ~WS_BORDER; //去除边框 return p; } }
方法二【优势,无边框;缺点:①操做费事些,代码多些 ②只能上下拖动大小,不能左右改变。③直接在要拖动的控件上操做,非拖动边框处(拖边框,只会改变此控件的大小,而且没法还原)】html
添加引用: ide
//using System.Runtime.InteropServices;
this
[DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); //常量 public const int WM_SYSCOMMAND = 0x0112; //窗体移动 public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); //常量 public const int WM_SYSCOMMAND = 0x0112; //窗体移动 public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; //改变窗体大小 public const int WMSZ_LEFT = 0xF001; public const int WMSZ_RIGHT = 0xF002; public const int WMSZ_TOP = 0xF003; public const int WMSZ_TOPLEFT = 0xF004; public const int WMSZ_TOPRIGHT = 0xF005; public const int WMSZ_BOTTOM = 0xF006; public const int WMSZ_BOTTOMLEFT = 0xF007; public const int WMSZ_BOTTOMRIGHT = 0xF008; //窗体移动 private void panel_title_bar_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } //窗体大小的改变 private void panel_bottom_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_BOTTOM, 0); } //改变窗体大小 public const int WMSZ_LEFT = 0xF001; public const int WMSZ_RIGHT = 0xF002; public const int WMSZ_TOP = 0xF003; public const int WMSZ_TOPLEFT = 0xF004; public const int WMSZ_TOPRIGHT = 0xF005; public const int WMSZ_BOTTOM = 0xF006; public const int WMSZ_BOTTOMLEFT = 0xF007; public const int WMSZ_BOTTOMRIGHT = 0xF008; //窗体移动 private void panel_title_bar_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } //窗体大小的改变 添加到一个控件上 private void panel_bottom_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_BOTTOM, 0); }寻找更好的方法中。。。