Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何拖动&;将PictureBox从一个Windows窗体拖放到另一个窗体_C#_Image_Forms_Drag And Drop_Picturebox - Fatal编程技术网

C# 如何拖动&;将PictureBox从一个Windows窗体拖放到另一个窗体

C# 如何拖动&;将PictureBox从一个Windows窗体拖放到另一个窗体,c#,image,forms,drag-and-drop,picturebox,C#,Image,Forms,Drag And Drop,Picturebox,我正在使用windows窗体构建一个简单的拖放游戏。这就像一个拼图游戏。我想把图片从一个表格拖到另一个表格来完成这个拼图。有没有一种方法可以用PictureBox做到这一点。它似乎是唯一没有AllowDrop函数的属性。下面是源代码 我想将图像从左边的窗体拖到右边的窗体 我一直在学习这个教程,但我无法理解它。 右表 using System; using System.Collections.Generic; using System.ComponentModel; using System

我正在使用windows窗体构建一个简单的拖放游戏。这就像一个拼图游戏。我想把图片从一个表格拖到另一个表格来完成这个拼图。有没有一种方法可以用PictureBox做到这一点。它似乎是唯一没有AllowDrop函数的属性。下面是源代码

我想将图像从左边的窗体拖到右边的窗体

我一直在学习这个教程,但我无法理解它。

右表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDropGame
{
    public partial class ChildFormTwo : Form
    {
        public ChildFormTwo()
        {
            InitializeComponent();

            pictureBox3.DragEnter += pictureBox3_DragEnter;
            pictureBox3.DragDrop += pictureBox3_DragDrop;
        }

        private void pictureBox3_DragDrop(object sender, DragEventArgs e)
        {
            var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox2.Image = bmp;
        }

        private void pictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effect = DragDropEffects.Move;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDropGame
{
    public partial class ChildFormTwo : Form
    {
        public ChildFormTwo()
        {
            InitializeComponent();

            pictureBox3.DragEnter += pictureBox3_DragEnter;
            pictureBox3.DragDrop += pictureBox3_DragDrop;
        }

        private void pictureBox3_DragDrop(object sender, DragEventArgs e)
        {
            var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox2.Image = bmp;
        }

        private void pictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effect = DragDropEffects.Move;
        }
    }
}
从左侧开始

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDropGame
{
    public partial class ChildFormTwo : Form
    {
        public ChildFormTwo()
        {
            InitializeComponent();

            pictureBox3.DragEnter += pictureBox3_DragEnter;
            pictureBox3.DragDrop += pictureBox3_DragDrop;
        }

        private void pictureBox3_DragDrop(object sender, DragEventArgs e)
        {
            var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox2.Image = bmp;
        }

        private void pictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effect = DragDropEffects.Move;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDropGame
{
    public partial class ChildFormTwo : Form
    {
        public ChildFormTwo()
        {
            InitializeComponent();

            pictureBox3.DragEnter += pictureBox3_DragEnter;
            pictureBox3.DragDrop += pictureBox3_DragDrop;
        }

        private void pictureBox3_DragDrop(object sender, DragEventArgs e)
        {
            var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox2.Image = bmp;
        }

        private void pictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effect = DragDropEffects.Move;
        }
    }
}

代码完全缺少DoDragDrop()方法调用。不要忽略其返回值。另请参见