当winform窗体的Bordestyle设置为None时,鼠标可以拖动窗体的办法
方法一:
1 2015-07-11 16:05:35 2 bool formMove = false;//窗体是否移动
3 Point formPoint;//记录窗体的位置 4 private void Form1_MouseDown(object sender, MouseEventArgs e) 5 { 6 formPoint = new Point(); 7 int xOffset; 8 int yOffset; 9 if (e.Button == MouseButtons.Left) 10 { 11 xOffset = -e.X; 12 yOffset = -e.Y; 13 formPoint = new Point(xOffset, yOffset); 14 formMove = true;//开始移动 15 } 16 } 17 18 private void Form1_MouseMove(object sender, MouseEventArgs e) 19 { 20 if (formMove == true) 21 { 22 Point mousePos = Control.MousePosition; 23 mousePos.Offset(formPoint.X, formPoint.Y); 24 Location = mousePos; 25 } 26 } 27 28 private void Form1_MouseUp(object sender, MouseEventArgs e) 29 { 30 if (e.Button == MouseButtons.Left)//按下的是鼠标左键 31 { 32 formMove = false;//停止移动 33 } 34 }
方法二:
1 Point mouseOff;//鼠标移动位置变量 2 bool leftFlag;//标签是否为左键 3 private void Login_MouseDown(object sender, MouseEventArgs e) 4 { 5 if (e.Button == MouseButtons.Left) 6 { 7 mouseOff = new Point(-e.X, -e.Y); //得到变量的值 8 leftFlag = true; //点击左键按下时标注为true; 9 } 10 } 11 12 private void Login_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 22 private void Login_MouseUp(object sender, MouseEventArgs e) 23 { 24 if (leftFlag) 25 { 26 leftFlag = false;//释放鼠标后标注为false; 27 } 28 }