我有 7 个图片框,我想拖放其中的每一个。我已经进行了拖放,但它带有我拖动的原始图片框,它不会留在原处。这是我的代码:
this.pbAND.MouseDown += pictureBox_MouseDown;
pbAND.MouseMove += pictureBox_MouseMove;
pbAND.MouseUp += pictureBox_MouseUp;
this.pbOR.MouseDown += pictureBox_MouseDown;
pbOR.MouseMove += pictureBox_MouseMove;
pbOR.MouseUp += pictureBox_MouseUp;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p = (PictureBox)sender;
downPoint = e.Location;
var dragImage = (Bitmap)p.Image;
IntPtr icon = dragImage.GetHicon();
Cursor.Current = new Cursor(icon);
p.Parent = this;
p.BringToFront();
DestroyIcon(icon);
}
}
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;
Control c = GetChildAtPoint(new Point(p.Left - 1, p.Top));
if (c == null) c = this;
Point newLoc = c.PointToClient(p.Parent.PointToScreen(p.Location));
p.Parent = c;
p.Location = newLoc;
}