c#怎样实现进程间的拖放操作
比如:在windows资源管理器中,选择几个文件,拖放到自己的程序中,把文件路径添加在程序的ListBox里

解决方案 »

  1.   

    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);
                }
            }
      

  2.   

    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;
            }