打赏

移动无边框窗体

开发环境:vs2010,步骤如下所示:

1、创建一个Windows应用程序,窗体默认Form1,将Form1的FormBorderStyle属性值设置为None

2、代码如下所示:

 1 namespace Kaifafanli
 2 {
 3     public partial class Form1 : Form
 4     {
 5         public Form1()
 6         {
 7             InitializeComponent();
 8         }
 9         //设置全局变量,初始化
10         bool beginMove = false;
11         int currentXPosition = 0;
12         int currentYPosition = 0;
13         private void Form1_MouseDown(object sender, MouseEventArgs e)
14         {
15             beginMove = true;
16             currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
17             currentYPosition = MousePosition.Y;
18         }
19 
20         private void Form1_MouseMove(object sender, MouseEventArgs e)
21         {
22             if(beginMove)
23             {
24                 this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
25                 this.Top += MousePosition.Y - currentYPosition;
26                 currentXPosition = MousePosition.X;
27                 currentYPosition = MousePosition.Y;
28             }
29         }
30 
31         private void Form1_MouseUp(object sender, MouseEventArgs e)
32         {
33             beginMove = false;
34         }
35 
36         private void Form1_MouseLeave(object sender, EventArgs e)
37         {
38             currentXPosition = 0;
39             currentYPosition = 0;
40             beginMove = false;
41         }        
42 
43        
44     }
45 }
View Code

 

posted @ 2016-06-30 14:53  学习靠自己  阅读(230)  评论(1编辑  收藏  举报