当我们需要美观页面,不需要C# winform自带的边框时,我们需要将FormBorderStyle属性设置为None,然后写入下面的鼠标事件,
然后就可以移动窗体
1 #region 窗体移动 2 private Point mouseOff;//鼠标移动位置变量 3 private bool leftFlag;//标签是否为左键 4 private void FrmMain_MouseDown(object sender, MouseEventArgs e) 5 { 6 if (e.Button == MouseButtons.Left) 7 { 8 mouseOff = new Point(-e.X, -e.Y); //得到变量的值 9 leftFlag = true; //点击左键按下时标注为true; 10 } 11 } 12 private void FrmMain_MouseMove(object sender, MouseEventArgs e) 13 { 14 if (leftFlag) 15 { 16 Point mouseSet = Control.MousePosition; 17 mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置 18 Location = mouseSet; 19 } 20 } 21 private void FrmMain_MouseUp(object sender, MouseEventArgs e) 22 { 23 if (leftFlag) 24 { 25 leftFlag = false;//释放鼠标后标注为false; 26 } 27 } 28 #endregion