Winform控件拖动
作者:互联网
原文链接:http://www.cnblogs.com/JoeDZ/archive/2009/06/18/1505555.html
给要实现拖动的控件添加如下的 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);
}
}
转载于:https://www.cnblogs.com/JoeDZ/archive/2009/06/18/1505555.html
标签:Control,控件,sender,ctrl,拖动,private,mousePositon,startPoint,Winform 来源: https://blog.csdn.net/weixin_30598225/article/details/98605062