窗体的拖动事件
标题框不显示:
将窗体的边框和外观属性 FormBorderStyle设为None
拖动方法如下:
为窗体添加一个私有域:
private System.Drawing.Point mousePoint;
为窗体添加鼠标按下事件(注意对鼠标按键进行筛选)
private void MainForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
this.mousePosition.X = e.X;
this.mousePosition.Y = e.Y;
}
}
为窗体添加鼠标移动事件处理方法:
private void MainForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Form.Top = Control.MousePosition.Y - mousePosition.Y - SystemInformation.FrameBorderSize.Height - SystemInformation.CaptionHeight;
Form.Left = Control.MousePosition.Y - mousePosition.Y - SystemInformation.FrameBorderSize.Width;
}
}
这里,如果窗体没有标题栏,可以去掉“-SystemInformation.CaptionHeight”;如果窗体没有边框,还可以去掉“-SystemInformation.FrameBorderSize.Height”和“-SystemInformation.FrameBorderSize.Width”。
如下:无标题栏和边框
private void MainForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Form.Top = Control.MousePosition.Y - mousePosition.Y;
Form.Left = Control.MousePosition.X - mousePosition.X;
}
}