#region 移动窗体 Point mouseOff;//鼠标移动位置变量 bool leftFlag;//标记是否为左键 private void Form_Home_ShowAlarm_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //得到变量的值 leftFlag = true; //点击左键按下时标注为true; } } private void Form_Home_ShowAlarm_MouseMove(object sender, MouseEventArgs e) { if (leftFlag) { Point mouseSet = Control.MousePosition; mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置 Location = mouseSet; } } private void Form_Home_ShowAlarm_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false;//释放鼠标后标注为false; } } #endregion
# region 无边框窗体的尺寸缩放 //using System.Runtime.InteropServices; [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 static int formResizeMode = 0; // 此处指定窗体尺寸修改的模式 // 在Form的MouseDown(object sender, MouseEventArgs e)事件中调用此函数,可调整窗体大小 public static void MouseDown_ResizeForm(Form form) { //常量 int WM_SYSCOMMAND = 0x0112; if (formResizeMode == 0) return; // 若无缩放模式,则返回 ReleaseCapture(); SendMessage(form.Handle, WM_SYSCOMMAND, formResizeMode, 0); } private void FormParent_MouseDown(object sender, MouseEventArgs e) { Form from = (Form)sender; MouseDown_ResizeForm(from); } // 在处于最前端的控件的MouseMove事件中调用此函数,根据鼠标位置设置对应的缩放模式 private void FormParent_MouseMove(object sender, MouseEventArgs e) { Control ctrl = (Control)sender; // 获取鼠标处的控件 Rectangle client = ctrl.ClientRectangle; // 获取控件尺寸 setSizeMode(client, new Point(e.X, e.Y), 10); // 根据鼠标在区域client中的位置设置缩放模式 } // 根据坐标 x,y 在rect上的位置控制尺寸调节模式 private static void setSizeMode(Rectangle R, Point P, int W) { formResizeMode = getSizeMode(R, P, W); } // 根据坐标 x,y 在rect上的位置控制尺寸调节模式 private static int getSizeMode(Rectangle R, Point P, int W) { //改变窗体大小相关参数 int WMSZ_LEFT = 0xF001; int WMSZ_RIGHT = 0xF002; int WMSZ_TOP = 0xF003; int WMSZ_TOPLEFT = 0xF004; int WMSZ_TOPRIGHT = 0xF005; int WMSZ_BOTTOM = 0xF006; int WMSZ_BOTTOMLEFT = 0xF007; int WMSZ_BOTTOMRIGHT = 0xF008; // 中间区域 Rectangle main = new Rectangle(R.Left + W, R.Top + W, R.Width - 2 * W, R.Height - 2 * W); if (main.Contains(P)) return 0; // 左侧区域 Rectangle LeftRect = new Rectangle(R.Left + W, R.Top + W, W, R.Height - 2 * W); if (LeftRect.Contains(P)) return WMSZ_LEFT; // 右侧区域 Rectangle RightRect = new Rectangle(R.Right - W, R.Top + W, W, R.Height - 2 * W); if (RightRect.Contains(P)) return WMSZ_RIGHT; // 顶部区域 Rectangle TopRect = new Rectangle(R.Left + W, R.Top, R.Width - 2 * W, W); if (TopRect.Contains(P)) return WMSZ_TOP; // 底部区域 Rectangle BottomRect = new Rectangle(R.Left + W, R.Bottom - W, R.Width - 2 * W, W); if (BottomRect.Contains(P)) return WMSZ_BOTTOM; // 左上区域 Rectangle TOPLEFT = new Rectangle(R.Left, R.Top, W, W); if (TOPLEFT.Contains(P)) return WMSZ_TOPLEFT; // 右上区域 Rectangle TOPRIGHT = new Rectangle(R.Right - W, R.Top, W, W); if (TOPRIGHT.Contains(P)) return WMSZ_TOPRIGHT; // 左下区域 Rectangle BOTTOMLEFT = new Rectangle(R.Left, R.Bottom - W, W, W); if (BOTTOMLEFT.Contains(P)) return WMSZ_BOTTOMLEFT; // 右下区域 Rectangle BOTTOMRIGHT = new Rectangle(R.Right - W, R.Bottom - W, W, W); if (BOTTOMRIGHT.Contains(P)) return WMSZ_BOTTOMRIGHT; return 0; }
//https://blog.csdn.net/scimence/article/details/51511245 # endregion
//拖动窗体/双击最大化/改变大小(无边框窗体)
//https://blog.csdn.net/szy759590387/article/details/88656971
欢迎加群交流 QQ群 830426796