-1

所以我有一个项目,我需要拖放不同的图片框,并在我拖放它们时将它们的现有副本制作到表单中。我的问题是,在表单上创建选定的“图片框”后,我无法移动它。我想有一个选项,我可以移动任何被拖动的图片框,而不是在一个位置拖动并将其保持在该位置。

    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:

    }
4

1 回答 1

0

将事件放入新的图片框

PB.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
PB.MouseDown += new MouseEventHandler(picturebOX_MouseDown);
PB.MouseUp += new MouseEventHandler(picturebOX_MouseUp);
于 2013-10-27T12:10:34.027 回答