前言
實現從窗口外部拖文件到窗口內部並自動捕獲文件地址。
第一步 開啟屬性
啟用底層Window的AllowDrop屬性,添加Drop事件。
Drop事件:當你拖動文件到對應控件后,松開觸發。
除
Drop事件外,我們還可以使用DragEnter、DragOver、DragLeave三個事件。
第二步 事件代碼
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);
}
