c#实现随鼠标移动窗体

  先声明
1 private  Point mousePoint;

 1          private   void  Form1_MouseMove( object  sender, MouseEventArgs e)
 2          {            
 3            if (e.Button == MouseButtons.Left)
 4            {
 5                this.Top = Control.MousePosition.Y - mousePoint.Y;
 6                this.Left = Control.MousePosition.X - mousePoint.X;
 7            }

 8        }

 9
10          private   void  Form1_MouseDown( object  sender, MouseEventArgs e)
11          {
12            if (e.Button == MouseButtons.Left)
13            {
14                this.mousePoint.X = e.X;
15                this.mousePoint.Y = e.Y;
16            }

17        }

如果窗体有标题
Top -= SystemInformation.CaptionHeight;

如果有边框
Top -= SystemInformation.FormBorderSize.Height
Left -= SystemInformation.FormBorderSize.Width

andyran

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