c#怎样实现进程间的拖放操作

canyangruxie 2010-10-20 06:14:08
c#怎样实现进程间的拖放操作
比如:在windows资源管理器中,选择几个文件,拖放到自己的程序中,把文件路径添加在程序的ListBox里
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-10-20
  • 打赏
  • 举报
回复
listbox拖放
private void listBox_DragDrop(object sender, DragEventArgs e)
{
ListBox destListBox = sender as ListBox;
if (e.Data.GetDataPresent(typeof(ListBox)))
{
ListBox srcListBox = e.Data.GetData(typeof(ListBox)) as ListBox;
destListBox.Items.Add(srcListBox.SelectedItem);
srcListBox.Items.Remove(srcListBox.SelectedItem);
}
}

private void listBox_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
324374 2010-10-20
  • 打赏
  • 举报
回复
ListBox可以实现这个功能,主要就是添加DragEnter,Drop事件。DrapEnter做拖放准备,Drop事件进行数据处理。不管是WPF还是Windows Forms的ListBox操作都差不多一样,下面的参考代码是WPF的。(注Windows Forms的ListBox要修改AllowDrop为true)

参考代码:
        private void ListBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
e.Effects = DragDropEffects.All;
}

private void ListBox_Drop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
listBox1.Items.Add(file);
}
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧