移动控件的问题
程序代码:
private bool isMouseDown = false; private Point mouseOffset; //记录鼠标指针的坐标 int button1_point = 10000000; private void button1_MouseDown(object sender, MouseEventArgs e) { if (button1.Left != button1_point && button1_point != 10000000) { isMouseDown = false; button1_point = 10000000; return; } if (e.Button == MouseButtons.Left) { button1_point = button1.Left; mouseOffset.X = e.X; mouseOffset.Y = e.Y; isMouseDown = true; } } private void button1_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { button1.BringToFront(); int left = button1.Left + e.X - mouseOffset.X; int top = button1.Top + e.Y - mouseOffset.Y; button1.Location = new Point(left, top); } }
用这种方法 移动控件 如果鼠标移动太快,会把 控件甩丢 有什么好的解决方案,控制 移动的控件必须跟紧鼠标 或者防止甩下移动的控件,就把对应移动的控件 返回到 原来的 Location里面