c#实现随鼠标移动窗体

窗体没有标题的情况下

代码
private Point mousePoint;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Top = Control.MousePosition.Y - mousePoint.Y;
this.Left = Control.MousePosition.X - mousePoint.X;
}
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.mousePoint.X = e.X;
this.mousePoint.Y = e.Y;
}
}

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

如果有边框
Top -= SystemInformation.FormBorderSize.Height
Left -= SystemInformation.FormBorderSize.Width
原文地址:https://www.cnblogs.com/wudingfeng/p/1703694.html