这个例子是将系统文件或目录拖动到窗体中,窗体以MessageBox的形式弹出用户拖入的文件或目录名称。

首先需要将要支持拖动的Form的AllowDrop=true;然后通过DragEnter和DragDrop事件即可,具体代码如下:

 1  private void Form1_DragDrop(object sender, DragEventArgs e)
 2         {
 3             System.Array datas = (System.Array)e.Data.GetData(DataFormats.FileDrop);
 4             string filePathOrDirectory = (datas).GetValue(0).ToString();
 5             if (Directory.Exists(filePathOrDirectory))
 6             {
 7                 MessageBox.Show("目录:" + filePathOrDirectory);
 8             }
 9             else
10             {
11                 MessageBox.Show("文件:" + filePathOrDirectory);
12             }
13         }
14 
15         private void Form1_DragEnter(object sender, DragEventArgs e)
16         {
17             if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
18                 e.Effect = DragDropEffects.Link;
19             else
20                 e.Effect = DragDropEffects.None;
21         }

 

相关文章:

  • 2021-10-05
  • 2021-09-07
  • 2021-06-09
  • 2021-12-13
  • 2021-12-11
  • 2021-05-22
  • 2021-12-28
  • 2022-12-23
猜你喜欢
  • 2021-06-19
  • 2021-08-03
  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案