winform 点击拖拽窗体
时间: 2023-05-22 09:06:12 浏览: 437
winform窗体拖拽控件
WinForm 中如何实现点击拖拽窗体?
回答:在 WinForm 中,可以通过鼠标事件来实现窗体的点击拖拽。具体的实现方法是,在窗体的 MouseDown、MouseMove、MouseUp 事件中,记录鼠标按下的位置,计算鼠标移动的距离,然后通过修改窗体的 Location 属性来实现窗体的拖拽。代码实现可以参考以下示例:
private Point lastPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lastPoint = new Point(e.X, e.Y);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + (e.X - lastPoint.X), this.Location.Y + (e.Y - lastPoint.Y));
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// do nothing
}
```
阅读全文