如何拖拽将PictureBox从一个Windows窗体拖放到另一个窗体

时间:2016-02-01 12:51:59

标签: c# image forms drag-and-drop picturebox

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

enter image description here

我想将图片从左侧的表单拖到右侧的表单。

我正在学习本教程,但我无法理解它。 C# Drag and Drop from one Picture box into Another

表格正确

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

0 个答案:

没有答案