|
比如winform 窗口上有個(gè)ListBox組件,我想拖動(dòng)系統(tǒng)中的文件夾到ListBox組件上,松開鼠標(biāo),ListBox能夠記錄顯示選中的文件夾的路徑,有了路徑,就可以處理路徑文件夾下的圖片、文本、視頻等內(nèi)容了。具體做法:
private void listBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
} 5.在DragDrop事件處理程序中,獲取拖放的文件夾路徑,并將其添加到ListBox中。 private void listBox_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] folders = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string folderPath in folders)
{
if (System.IO.Directory.Exists(folderPath))
{
listBox.Items.Add(folderPath);
}
}
}
}這段代碼將遍歷拖放的文件夾路徑數(shù)組,并將每個(gè)存在的文件夾路徑添加到ListBox中。 6.可選:在DragOver事件處理程序中提供視覺反饋,比如更改鼠標(biāo)指針的外觀。 private void listBox_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}這段代碼將在拖動(dòng)操作在ListBox上進(jìn)行時(shí),將鼠標(biāo)指針的效果設(shè)置為拷貝。 現(xiàn)在我們的的WinForms窗口上的ListBox組件應(yīng)該可以接收系統(tǒng)中的文件夾的拖放操作,并將選中文件夾的路徑記錄并顯示在ListBox中。 |
|
|