Winform 拖放

  • 拖放操作向来比较受青睐,因为它操作简单,符合Windows用户的操作习惯,那么在WinForms里如何实现拖放呢?下面我将以最简单的两个PictureBox控件间的图片相互拖放来讲解。

    实现拖放有几个注意点:

    第一:被拖放对象的 mousedown事件,触发DoDragDrop 事件(codpy或者move)

    第二:要托放进的控件的 AllowDrop = true(如果智能提示不出现,就需要将这个设置直接手写出来,不清楚微软为什么不给智能提示)

    第三:实现 DragEnter 事件(判断拖放进来的控件,是否是自己需要的格式)

    第四:实现 DragDrop 事件(处理的代码)

    要实现两个PictureBox控件间的图片相互拖放,只需要以下4步: 
    (1)
     先设置两个PictureBox控件的AllowDrop属性(注意:PictureBox控件的属性列表里没有AllowDrop属性,我们可以通过代码来设置。但是在代码设置的时候,还是发现点不出来,这是正常的,我们硬设置进去)。
    关键代码为:
        this.picLeft.AllowDrop = true;
        this.picRight.AllowDrop = true;
    
    
    (2) 在两个PictureBox控件的MouseDown事件里开始拖放数据(这里是PictureBox显示的图片)。
    关键代码为:
                //判断是否按下鼠标左键
                if (e.Button == MouseButtons.Left)
                {
                    //从sender对象里取出源事件PictureBox控件
                    PictureBox picTemp = sender as PictureBox;
                    //判断事件源对象是否正显示了图片
                    if (picTemp.Image != null)
                    {
                        //开始拖放操作,并传递要拖动的数据以及拖放的类型(是移动操作还是拷贝操作)
                        picTemp.DoDragDrop(picTemp.Image,DragDropEffects.Move | DragDropEffects.Copy);
                    }
                }
    (3) 在两个PictureBox控件的DragEnter事件里检查拖放的数据。
    关键代码为:
                //检查拖动的数据是否适合于目标空间类型;若不适合,则拒绝放置。
                if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
                    //检查Ctrl键是否被按住
                    if (e.KeyState == Ctrl) {
                        //若Ctrl键被按住,则设置拖放类型为拷贝
                        e.Effect = DragDropEffects.Copy;
                    }
                    else
                    {
                        //若Ctrl键没有被按住,则设置拖放类型为移动
                        e.Effect = DragDropEffects.Move;
                    }
                }
                else
                {
                    //若被拖动的数据不适合于目标控件,则设置拖放类型为拒绝放置
                    e.Effect = DragDropEffects.None;
                }
    (4) 在两个PictureBox控件的DragDrop事件里完成数据的拖放。
    关键代码为:
                //从sender对象里取出目标事件PictureBox控件
                PictureBox picTemp = sender as PictureBox;
                //从事件参数中取出拖动的数据,并转换为图象对象
                picTemp.Image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
                //若不是执行的拷贝操作
                if (e.KeyState != Ctrl)
                {
                    //且是把图片放置到picLeft控件,那么picRight控件显示的图片将被清除;反之亦然。
                    if (picTemp.Name == "picLeft")
                    {
                        this.picRight.Image = null;
                    }
                    else
                    {
                        this.picLeft.Image = null;
                    }
                }

    整个拖放操作完整代码为:

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

    namespace ActionDemo
    {
        public partial class frmAction : Form
        {

            private const byte Ctrl = 8;//表示按住了Ctrl键(类似的还有Alt、Shift、鼠标左键、鼠标右键等键的常数定义,请查看MSDN)
            public frmAction()
            {
                InitializeComponent();
            }

            //窗体加载事件
            private void frmAction_Load(object sender, EventArgs e)
            {
                this.picLeft.Image = Image.FromFile("01.jpg");
                this.picLeft.AllowDrop = true;
                this.picRight.AllowDrop = true;

                //绑定提示信息
                this.tlPMessage.SetToolTip(this.picLeft, "同时按住Ctrl键将复制图片");
                this.tlPMessage.SetToolTip(this.picRight, "同时按住Ctrl键将复制图片");
            }

            //鼠标按下事件
            private void picLeft_MouseDown(object sender, MouseEventArgs e)
            {
                //判断是否按下鼠标左键
                if (e.Button == MouseButtons.Left)
                {
                    //从sender对象里取出源事件PictureBox控件
                    PictureBox picTemp = sender as PictureBox;
                    //判断事件源对象是否正显示了图片
                    if (picTemp.Image != null)
                    {
                        //开始拖放操作,并传递要拖动的数据以及拖放的类型(是移动操作还是拷贝操作)
                        picTemp.DoDragDrop(picTemp.Image,DragDropEffects.Move | DragDropEffects.Copy);
                    }
                }
            }

            //在用鼠标将某项拖放到该控件的工作区时发生
            private void picLeft_DragEnter(object sender, DragEventArgs e)
            {
                //检查拖动的数据是否适合于目标空间类型;若不适合,则拒绝放置。
                if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
                    //检查Ctrl键是否被按住
                    if (e.KeyState == Ctrl) {
                        //若Ctrl键被按住,则设置拖放类型为拷贝
                        e.Effect = DragDropEffects.Copy;
                    }
                    else
                    {
                        //若Ctrl键没有被按住,则设置拖放类型为移动
                        e.Effect = DragDropEffects.Move;
                    }
                }
                else
                {
                    //若被拖动的数据不适合于目标控件,则设置拖放类型为拒绝放置
                    e.Effect = DragDropEffects.None;
                }
            }

            //拖放操作完成时发生
            private void picLeft_DragDrop(object sender, DragEventArgs e)
            {
                //从sender对象里取出目标事件PictureBox控件
                PictureBox picTemp = sender as PictureBox;
                //从事件参数中取出拖动的数据,并转换为图象对象
                picTemp.Image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
                //若不是执行的拷贝操作
                if (e.KeyState != Ctrl)
                {
                    //且是把图片放置到picLeft控件,那么picRight控件显示的图片将被清除;反之亦然。
                    if (picTemp.Name == "picLeft")
                    {
                        this.picRight.Image = null;
                    }
                    else
                    {
                        this.picLeft.Image = null;
                    }
                }
            }
        }
    }

        //this.DoDragDrop(sender, DragDropEffects.Copy | DragDropEffects.Move);
                this.DoDragDrop(sender, DragDropEffects.Move);

                //判断拖入的对象格式
                //e.Data.GetDataPresent(typeof(System.String))
                //(e.Data.GetDataPresent(DataFormats.Text)
                if (e.Data.GetDataPresent(typeof(BlockItem)) == true)
             

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm中向窗体中拖放图片,可以通过以下步骤实现: 1. 在窗体上添加一个PictureBox控件,用于显示拖放的图片。 2. 启用窗体的拖放功能。在窗体的构造函数或Load事件中,添加以下代码: this.AllowDrop = true; 3. 实现窗体的DragEnter事件,用于判断拖放的内容是否为图片,并设置拖放效果。代码示例如下: private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); // 判断拖放的内容是否为图片格式 if (files.Length > 0 && new List<string> { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }.Contains(Path.GetExtension(files[0]).ToLower())) { e.Effect = DragDropEffects.Copy; } } } 4. 实现窗体的DragDrop事件,用于获取拖放的图片并在PictureBox中显示。代码示例如下: private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); // 获取拖放的第一张图片 if (files.Length > 0) { string imagePath = files[0]; if (new List<string> { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }.Contains(Path.GetExtension(imagePath.ToLower()))) { // 显示图片 pictureBox1.Image = Image.FromFile(imagePath); } } } } 通过以上步骤,当用户拖放图片文件到窗体上时,会将文件的路径读取出来,并判断是否为图片格式,然后将图片显示在PictureBox控件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值