`
hzy3774
  • 浏览: 984850 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

C#窗体程序实现文件拖放

 
阅读更多

C#实现文件拖放首先要设置窗体的可拖放为True:



 然后重写DragEnter和DragDrop事件:

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

namespace DragAndDrop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            if (fileNames.Length > 0)
                pictureBoxPic.Load(fileNames[0]);
        }
    }
}

 该程序实现显示任何拖到窗体上的图片:




 
 (string[])e.Data.GetData(DataFormats.FileDrop, false)返回一个数组;如果拖放了多个文件,数组里就会有多个元素。

  • 大小: 68.9 KB
  • 大小: 921.7 KB
分享到:
评论
1 楼 hyghyg1234 2016-06-13  
  

相关推荐

Global site tag (gtag.js) - Google Analytics