C# 拖放重新选择图片框以从窗体中移动
本文关键字:窗体 移动 拖放 新选择 选择 | 更新日期: 2023-09-27 18:37:10
所以我有一个项目,我需要拖放不同的图片框,并在拖放它们时将它们的现有副本复制到表单中。我的问题是在表单上创建选定的"图片框"后,我无法移动它。我想要一个选项,我可以移动任何拖动的图片框,而不是像拖动某个位置并将其保持在该位置一样。
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p = (PictureBox)sender;
p.Tag = p.Location;
downPoint = e.Location;
p.Parent = this;
p.BringToFront();
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
if (e.Button == MouseButtons.Left)
{
p.Left += e.X - downPoint.X ;
p.Top += e.Y - downPoint.Y ;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
PictureBox PB = new PictureBox();
Control c = GetChildAtPoint(new Point(p.Left -1, p.Top));
if (c == null) c = this;
Point newLoc = c.PointToClient(p.Parent.PointToScreen(p.Location));
PB.Parent = c;
PB.Location = newLoc;
;
p.Parent.Controls.Add(PB); // <-- add new PB to the form!
p.Location = (Point)p.Tag;
// put the original back where it started:
}
将事件放入新图片框
PB.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
PB.MouseDown += new MouseEventHandler(picturebOX_MouseDown);
PB.MouseUp += new MouseEventHandler(picturebOX_MouseUp);