0

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;
    }
4

1 に答える 1

0

しかし、ドラッグした元のピクチャボックスがそのまま残りません。

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;
}
于 2013-10-26T18:12:32.203 に答える