Point lastPoint;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Label labNew = new Label();
labNew.Name = "aaa";
labNew.Text = "新标签";
this.Controls.Add(labNew);
labNew.MouseDown +=new MouseEventHandler(labNew_MouseDown);
labNew.MouseMove +=new MouseEventHandler(labNew_MouseMove);
}
private void labNew_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
lastPoint = e.Location;
}
private void labNew_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Label l = (Label)sender;
l.Location = new Point(l.Location.X + e.Location.X - lastPoint.X,
l.Location.Y + e.Location.Y - lastPoint.Y);
}
}
C# WinForm VS2005 动态添加并拖动控件
最新推荐文章于 2023-09-14 15:49:57 发布