C# 拖放picturebox将消失

C# 拖放picturebox将消失,c#,winforms,drag-and-drop,picturebox,C#,Winforms,Drag And Drop,Picturebox,我的代码有问题。我正试图在表单上拖放图片,但当我移动选定的图片框时,当我走出组框时,它就会丢失。它就这样消失了 public partial class Form1 : Form { int x_offset = 0; // any better to do this without having a global variable? int y_offset = 0; PictureBox dpb = new PictureBox(); public Form

我的代码有问题。我正试图在表单上拖放图片,但当我移动选定的图片框时,当我走出组框时,它就会丢失。它就这样消失了

public partial class Form1 : Form
{
    int x_offset = 0; // any better to do this without having a global variable?
    int y_offset = 0;

    PictureBox dpb = new PictureBox();
    public Form1()
    {
        InitializeComponent();

        this.WindowState = FormWindowState.Maximized;
        this.AllowDrop = true;
        this.pictureBox1.MouseDown += pictureBox1_MouseDown;
        this.pictureBox2.MouseDown += pictureBox2_MouseDown;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox me = (PictureBox)sender;
        x_offset = e.X;
        y_offset = e.Y;

    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            PictureBox me = (PictureBox)sender;
            me.Left = e.X + me.Left - x_offset;
            me.Top = e.Y + me.Top - y_offset;
        }
    }

您的PictureBox正在被父项(即GroupBox)剪裁。您可以修复层次结构(视图->其他窗口->文档大纲)


此外,通常最好使用标准的拖放功能,如下所述:。这将处理所有拖放特殊情况。要更改标准游标,请将cursor.Current设置为由CreateCursor(myBitmap)返回的游标。注意:CreateCursor在某些情况下可能会失败,因此请确保提供标准游标的回退。

您的
PictureBox
有一个
GroupBox
作为其父控件,在
winforms
和许多其他UI技术中,您不能在其父控件之外呈现子控件。在使用代码之前,您可能需要执行以下操作:

pictureBox1.Parent = this;//set your Form as Parent of the pictureBox1
pictureBox1.BringToFront();//Ensure your pictureBox1 is on top.
如果您需要将
pictureBox1
GroupBox
拖放到另一个控件上,使其成为
pictureBox1
的新
父控件,您可以尝试以下代码:

    Point downPoint;
    //MouseDown event handler for your pictureBox1
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e){
        downPoint = e.Location;
        pictureBox1.Parent = this;
        pictureBox1.BringToFront();
    }
    //MouseMove event handler for your pictureBox1
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e){
        if (e.Button == MouseButtons.Left) {
            pictureBox1.Left += e.X - downPoint.X;
            pictureBox1.Top += e.Y - downPoint.Y;
        }
    }
    //MouseUp event handler for your pictureBox1
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
        Control c = GetChildAtPoint(new Point(pictureBox1.Left - 1, pictureBox1.Top));
        if (c == null) c = this;
        Point newLoc = c.PointToClient(pictureBox1.Parent.PointToScreen(pictureBox1.Location));
        pictureBox1.Parent = c;
        pictureBox1.Location = newLoc;
    }

欢迎来到堆栈溢出!最好在你的答案中总结一下链接背后的内容,这样当链接过时时就不会丢失。否则,你的第一个答案做得很好。thanx关于你的信息很多,但是我需要将选中的图片框保留在它的位置,只需拖放图像的副本。所以我的公式是,我如何离开原始的图片盒,只拿一份我将要拖动的副本。问候:)@ToshkoKosev您的问题和您的代码都表明您想移动
pictureBox
,您甚至没有提及与您在评论中所说内容相关的任何内容。