Winform控件拖动
给要实现拖动的控件添加如下的 MouseDown 和 MouseMove 事件即可。
private Point startPoint;
private void MouseDown(object sender, MouseEventArgs e) {
startPoint.X = e.X;
startPoint.Y = e.Y;
}
private void MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Point mousePositon = Control.MousePosition;
mousePositon.Offset(-startPoint.X, -startPoint.Y);
Control ctrl = (Control)sender;
ctrl.Location = ctrl.Parent.PointToClient(mousePositon);
}
}
private void MouseDown(object sender, MouseEventArgs e) {
startPoint.X = e.X;
startPoint.Y = e.Y;
}
private void MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Point mousePositon = Control.MousePosition;
mousePositon.Offset(-startPoint.X, -startPoint.Y);
Control ctrl = (Control)sender;
ctrl.Location = ctrl.Parent.PointToClient(mousePositon);
}
}