当前位置:首页 > 开发 > C# > 正文内容

C#Winform接受拖放文件

C#1天前
        public Form1()
        {
            InitializeComponent();

            this.AllowDrop = true; // 关键步骤1:允许窗体接受拖放
            this.DragEnter += new DragEventHandler(MainForm_DragEnter);
            this.DragDrop += new DragEventHandler(MainForm_DragDrop);
        }

        private void MainForm_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))// 关键步骤2:检查拖入的是否是文件
            {
                e.Effect = DragDropEffects.Copy; // 设置鼠标为“复制”效果
            }
        }

        private void MainForm_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);// 关键步骤3:用户松开鼠标后,获取文件路径
            foreach (string file in files)
            {
                listBox1.Items.Add(file); // 将每个文件的路径添加到listBox1
            }
        }


转载请注明出处。

本文链接:http://pythonopen.com/?id=7936

相关文章

C# 热键

API     /// <summary>...

C# 可空参数

using System; using System.Runtime.Inte...

C# BackgroundWorker

1.概述BackgroundWorker是一个在 WinForms 应用程序中用于简化在后台线程执行...

C# BackgroundWorker的例子

以下是一个使用 BackgroundWorker 组件在 C# 中实现后台执行任务,同时在主线程更新...

C# using与多重using

1. using 语句概述在 C# 中,using 语句主要用于确保实现了 IDisposable...