如何将文件拖放到应用程序中? [英] How do I drag and drop files into an application?

查看:260
本文介绍了如何将文件拖放到应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Borland的 Turbo C ++ 环境中看到了这一点,但我不确定如何解决它正在开发的C#应用​​程序。有没有最好的做法或疑虑来寻找?

解决方案

一些示例代码:

  public partial class Form1:Form {
public Form1(){
InitializeComponent();
this.AllowDrop = true;
this.DragEnter + = new DragEventHandler(Form1_DragEnter);
this.DragDrop + = new DragEventHandler(Form1_DragDrop);
}

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

void Form1_DragDrop(object sender,DragEventArgs e){
string [] files =(string [])e.Data.GetData(DataFormats.FileDrop);
foreach(文件中的字符串文件)Console.WriteLine(file);
}
}


I've seen this done in Borland's Turbo C++ environment, but I'm not sure how to go about it for a C# application I'm working on. Are there best practices or gotchas to look out for?

解决方案

Some sample code:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      this.AllowDrop = true;
      this.DragEnter += new DragEventHandler(Form1_DragEnter);
      this.DragDrop += new DragEventHandler(Form1_DragDrop);
    }

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

    void Form1_DragDrop(object sender, DragEventArgs e) {
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      foreach (string file in files) Console.WriteLine(file);
    }
  }

这篇关于如何将文件拖放到应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆