c#实现随鼠标移动窗体

  先声明
1private 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

posted on 2007-12-05 10:49  andyran  阅读(1599)  评论(0编辑  收藏  举报

导航