将文件拖到窗体上以打开文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将文件拖到窗体上以打开文件相关的知识,希望对你有一定的参考价值。

Example written 01/13/2011

The following code shows how to implement functionality in your Windows Forms application, such that you'll be able to open a file that is dragged and dropped onto your program's form. This behavior is often desirable for programs that manipulate files in some way (renamers, encrypters, etc.) It's also nice for the uer to be able to have more than one way to open a file. This example implements opening via drag-dropping, and via a customary File->Open menu.
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Diagnostics;
  4. using System.IO;
  5.  
  6. namespace BryanWinForms1 {
  7. public partial class Form1 : Form {
  8.  
  9. private delegate void DelegateOpenFile(String s); //define delegate type
  10. DelegateOpenFile _openFileDelegate; //declares a delegate instance
  11.  
  12. public Form1() {
  13. InitializeComponent();
  14. this.AllowDrop = true; //must set this to true, for dragDrop to work
  15. //delegate needed so Form1_DragDrop() can asynchronously
  16. //invoke our program's OpenFile() method
  17. _openFileDelegate = new DelegateOpenFile(this.OpenFile); //instantiates delegate
  18. }
  19.  
  20. private void openToolStripMenuItem_Click(object sender, EventArgs e) {
  21. OpenFileDialog openDlg = new OpenFileDialog();
  22. openDlg.Filter = "Any File (*.*)|*.*";
  23. openDlg.FileName = "";
  24. openDlg.CheckFileExists = true;
  25. openDlg.CheckPathExists = true;
  26.  
  27. if (openDlg.ShowDialog() != DialogResult.OK)
  28. return;
  29.  
  30. OpenFile(openDlg.FileName);
  31. }
  32.  
  33. private void OpenFile(string sFile) {
  34. //insert appropriate file-opening code here...
  35. MessageBox.Show(""" + sFile + "" will be opened.");
  36. }
  37.  
  38. private void Form1_DragEnter(object sender, DragEventArgs e) {
  39. //we're only interested if a FILE was dropped on the form
  40. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  41. e.Effect = DragDropEffects.Copy;
  42. else
  43. e.Effect = DragDropEffects.None;
  44. }
  45.  
  46. private void Form1_DragDrop(object sender, DragEventArgs e) {
  47. //good idea to use try-catch block, in case something goes wrong
  48. try {
  49. Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
  50. if (a != null) {
  51. // Extract string from first array element
  52. // (ignore all files except first if number of files are dropped).
  53. string s = a.GetValue(0).ToString();
  54. // Call OpenFile asynchronously.
  55. // Explorer instance from which file is dropped is not responding
  56. // the entire time that the DragDrop handler is active, so we need to return
  57. // immidiately (especially if OpenFile shows MessageBox).
  58. this.BeginInvoke(_openFileDelegate, new Object[] { s });
  59. this.Activate(); // in the case Explorer overlaps this form
  60. }
  61. }
  62. catch (Exception ex) {
  63. Trace.WriteLine("Error in DragDrop function: " + ex.Message);
  64. // don't show MessageBox here - Explorer is waiting !
  65. }
  66. }
  67.  
  68. //next right-brace ends Form1 class
  69. }
  70. //next right-brace ends namespace
  71. }

以上是关于将文件拖到窗体上以打开文件的主要内容,如果未能解决你的问题,请参考以下文章

获取拖入 Windows 窗体窗体的文件的路径

c#如何实现关闭当前窗体并打开另一个已经创建的窗体。

窗体设置操作

使用 Selenium 模拟将文件拖到上传元素上

是啥使得可以在 Mac 上拖动打开文件?

我在哪里更改此 Python 代码片段以将临时文件保存在 tmp 文件夹中?