去掉form的窗体的边框后无法拖动窗体,下面简单的代码即可实现;
如何去掉边框详见:http://www.cnblogs.com/lgx040605112/archive/2012/10/08/2715438.html
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace WindowsApplication1 10 { 11 public partial class Form1 : Form 12 { 13 public Form1() 14 { 15 InitializeComponent(); 16 } 17 18 private Point mPoint = new Point(); 19 20 private void Form1_MouseDown(object sender, MouseEventArgs e) 21 { 22 mPoint.X = e.X; 23 mPoint.Y = e.Y; 24 } 25 26 private void Form1_MouseMove(object sender, MouseEventArgs e) 27 { 28 if (e.Button == MouseButtons.Left) 29 { 30 Point myPosittion = MousePosition; 31 myPosittion.Offset(-mPoint.X, -mPoint.Y); 32 Location = myPosittion; 33 } 34 } 35 } 36 }
另外一种实现方式:
View Code
1 public partial class NoBorderForm : Form 2 { 3 /// <summary> 4 /// 鼠标是否按下 5 /// </summary> 6 private bool mouseDown = false; 7 8 /// <summary> 9 /// 初始移动位置 10 /// </summary> 11 private Point movePoint; 12 13 public NoBorderForm() 14 { 15 InitializeComponent(); 16 17 MouseDown += new MouseEventHandler(NoBorderForm_MouseDown); 18 MouseMove += new MouseEventHandler(NoBorderForm_MouseMove); 19 MouseUp += new MouseEventHandler(NoBorderForm_MouseUp); 20 } 21 22 void NoBorderForm_MouseUp(object sender, MouseEventArgs e) 23 { 24 if (mouseDown) 25 { 26 mouseDown = false; 27 } 28 } 29 30 void NoBorderForm_MouseMove(object sender, MouseEventArgs e) 31 { 32 if (mouseDown) 33 { 34 Point curPoint = Cursor.Position; 35 //Point curPoint = MousePosition; 36 int x = curPoint.X - movePoint.X; 37 int y = curPoint.Y - movePoint.Y; 38 movePoint = curPoint; 39 Location = new Point(Location.X + x, Location.Y + y); 40 } 41 } 42 43 void NoBorderForm_MouseDown(object sender, MouseEventArgs e) 44 { 45 if (e.Button == MouseButtons.Left) 46 { 47 mouseDown = true; 48 movePoint = Cursor.Position; 49 //movePoint = MousePosition; 50 } 51 } 52 }