c# 如何按住一个控件时,可以利用这个控件移动窗体位置

2024-12-16 09:30:33
推荐回答(1个)
回答1:

private Point mouse_offset; //记录位置
private void button1_MouseDown(object sender, MouseEventArgs e)
{
mouse_offset = new Point(-e.X, -e.Y);
}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) //如果是左击鼠标
{
Point mousePos = Control.MousePosition; //获取鼠标的位置
mousePos.Offset(mouse_offset.X, mouse_offset.Y);//设置偏移
this.button1.Location = this.PointToClient(mousePos);
}
}