前言

实现从窗口外部拖文件到窗口内部并自动捕获文件地址。

第一步 开启属性

启用底层WindowAllowDrop属性,添加Drop事件。

Drop事件:当你拖动文件到对应控件后,松开触发。

Drop事件外,我们还可以使用DragEnterDragOverDragLeave三个事件。

第二步 事件代码

private void MainWindow_Drop(object sender, DragEventArgs e)
{
    string msg = "Drop";
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        msg = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
    }

    MessageBox.Show(msg);
}

相关文章:

  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
猜你喜欢
  • 2022-01-14
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案