如何将图片框移出面板?

时间:2018-03-31 19:36:50

标签: c# visual-studio

我在面板中有图片框,我想用鼠标移动这个图片框。 这个脚本运行良好,但我只能在这个面板中移动它。如何将其移至表格或其他面板?

    Point firstPoint = new Point();

    public void MoveImage()
    {
        pictureBox1.MouseDown += (ss, ee) =>
        {
            if (ee.Button == System.Windows.Forms.MouseButtons.Left)
            {
                firstPoint = Control.MousePosition;
            }
        };
        pictureBox1.MouseMove += (ss, ee) =>
        {
            if (ee.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Point temp = Control.MousePosition;
                Point res = new Point(firstPoint.X - temp.X, firstPoint.Y - temp.Y);
                pictureBox1.Location = new Point(pictureBox1.Location.X - res.X, pictureBox1.Location.Y - res.Y);
                firstPoint = temp;
            }
        };
    }

1 个答案:

答案 0 :(得分:0)

将从容器中显示控件的示例代码,请根据需要进行修改或将其添加到代码中:

    int i = 0;

    void moveout()

    {          
        if (i % 2 == 0)

        {

            // if the control is in a container control

            if (picturebox.Parent.Name!= this.Name)

            {          

                Point moveLocation = new Point(picturebox.Location.X + picturebox.Parent.Location.X,                                                              picturebox.Location.Y + picturebox.Parent.Location.Y);

                // remove the control first

                picturebox.Parent.Controls.Remove(picturebox);

                this.Controls.Add(picturebox);

                picturebox.Location = moveLocation;

            }       
            picturebox.SendToBack();
        }
        else
        {
            picturebox.BringToFront();
        }
    ++i;
    }

Source

希望这有效。如果没有,请在downvoting之前留言:)