【问题标题】:C# drag and drop pictureboxC#拖放图片框
【发布时间】:2013-11-05 16:48:03
【问题描述】:

我有 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;
    }

【问题讨论】:

  • 不清楚你想用拖放做什么。您的代码会更改相应图片框的位置,因此它们不会留在原地也就不足为奇了。
  • 所说的拖放,是指改变它们在表单中的位置吗?

标签: c# drag-and-drop picturebox


【解决方案1】:

但它带有我拖动的原始图片框 把它留在原处。

所以你想在 PictureBox 被放下时制作一个副本

在 MouseDown() 处理程序中,将原始位置存储在 Tag() 属性中:

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        p = (PictureBox)sender;
        p.Tag = p.Location; // <-- store the Location in the Tag() property

        // ... rest of the existing code ...

    }
}

在 MouseUp() 处理程序中,将 New PictureBox 放在当前位置并重置原始位置:

private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
    p = (PictureBox)sender;

    // create a new PictureBox that looks like the original:
    PictureBox PB = new PictureBox();
    PB.Size = p.Size;
    PB.Image = p.Image;
    PB.SizeMode = p.SizeMode;
    PB.BorderStyle = p.BorderStyle;
    // etc...make it look the same

    // ...and place it:
    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!

    // put the original back where it started:
    p.Location = (Point)p.Tag;
}

【讨论】:

  • 我想拖动一个图片框,但不是原始图片框,而是它的副本,我想在使用 mouse_down 事件时制作图片框的精确副本。现在,当我拖放时,我使用moue down,它采用原始图片框而不是副本,因此我可以拥有一组固定的图片框,我可以选择通过拖放来制作它们的副本。善良的 ragrds 和非常感谢您尝试 helo :)
  • 您是否尝试过上述方法?...应该非常接近。它会让你拖动原件,但是当它被放下时,它会制作一个副本并将原件放回去。在保留原件的同时制作副本并拖动它有点棘手。
  • 是的,但是当我拖动它时它不会复制我选择拖动的图片框,而是将选定的图片框返回到初始位置。
  • 糟糕!...我忘记将新的图片框添加到表单中。查看更新。
  • 我看到了,当我现在尝试时,它显示错误“对象引用未设置为对象的实例。”对于 p.Location = (Point)p.Tag;行。
猜你喜欢
  • 2023-03-20
  • 2013-04-06
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多