.net中实现拖拽控件
private int tX = 0;
private int tY = 0;
private void btn_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.tX = e.X;
this.tY = e.Y;
this.btn.MouseMove += new System.Windows.Forms.MouseEventHandler(this.btn_MouseMove);
}
private void btn_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.btn.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.btn_MouseMove);
}
private void btn_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.btn.Location = new System.Drawing.Point(this.btn.Location.X + e.X - this.tX, this.btn.Location.Y + e.Y - this.tY);
}
private int tY = 0;
private void btn_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.tX = e.X;
this.tY = e.Y;
this.btn.MouseMove += new System.Windows.Forms.MouseEventHandler(this.btn_MouseMove);
}
private void btn_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.btn.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.btn_MouseMove);
}
private void btn_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.btn.Location = new System.Drawing.Point(this.btn.Location.X + e.X - this.tX, this.btn.Location.Y + e.Y - this.tY);
}