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 }
时间: 2024-10-11 07:52:55