C#中无边框窗体移动

拖动无边框窗体Form至桌面任何位置

首先建一个Windows应用程序
将Form1的 FormBorderStyle属性设置为Noe

 1  Point mouseOff;//鼠标移动位置变量

 2         bool leftFlag;//标记是否为左键

 3         private void Form1_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 Form1_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 Form1_MouseUp(object sender, MouseEventArgs e)

23         {

24             if (leftFlag)

25             {

26                 leftFlag = false;//释放鼠标后标注为false;

27             }

28         }

 

你可能感兴趣的:(C#)