2010-01-17 108 views
2

1)如何使用拖动&拖放来移动ListView中的项目?我已经实施D & D副本以查找从目录中拖动的文件。C#拖放当前列表视图中的项目移动

2)顺便说一句,你怎么用d得到一个目录链接& d到ListView中,我见过的应用程序,从Windows资源管理器的地址栏中获得通过d & d的目录路径。

private void lvwFiles_DragEnter(object sender, DragEventArgs e) 
{ 
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
     e.Effect = DragDropEffects.Copy; 
    else 
     e.Effect = DragDropEffects.None; 
} 

private void lvwFiles_DragDrop(object sender, DragEventArgs e) 
{ 
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
    { 
     var paths = (string[])e.Data.GetData(DataFormats.FileDrop); 
     var path = Path.GetDirectoryName(paths[0]); 
     paths = Media.FilterPaths(paths); 
     lvwFilesAdd(path, paths); 
     lvwFilesWrite(); 
    } 
} 

发现这个@微软(VS2005; http://support.microsoft.com/kb/822483),我试图让这个代码的意义,使之在我的计划工作。我将看看如何将这与DragEnter & DragDrop事件处理程序中已有的代码分开。

//lvwFiles_ItemDrag event handler 
// 
//Begins a drag-and-drop operation in the ListView control. 
lvwFiles.DoDragDrop(lvwFiles.SelectedItems, DragDropEffects.Move); 

//lvwFiles_DragEnter event handler 
// 
int len=e.Data.GetFormats().Length-1 ; 
int i; 
for (i = 0 ; i<=len ;i++) 
{ 
    if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection")) 
    { 
     //The data from the drag source is moved to the target. 
     e.Effect = DragDropEffects.Move; 
    } 
} 

//lvwFiles_DragDrop event handler 
// 
//Return if the items are not selected in the ListView control. 
if(lvwFiles.SelectedItems.Count==0) 
{ 
    return; 
} 
//Returns the location of the mouse pointer in the ListView control. 
Point cp = lvwFiles.PointToClient(new Point(e.X, e.Y)); 
//Obtain the item that is located at the specified location of the mouse pointer. 
ListViewItem dragToItem = lvwFiles.GetItemAt(cp.X, cp.Y); 
if(dragToItem==null) 
{ 
    return; 
} 
//Obtain the index of the item at the mouse pointer. 
int dragIndex = dragToItem.Index; 
ListViewItem[] sel=new ListViewItem [lvwFiles.SelectedItems.Count]; 
for(int i=0; i<=lvwFiles.SelectedItems.Count-1;i++) 
{ 
    sel[i]=lvwFiles.SelectedItems[i]; 
} 
for(int i=0; i<sel.GetLength(0);i++) 
{ 
    //Obtain the ListViewItem to be dragged to the target location. 
    ListViewItem dragItem = sel[i]; 
    int itemIndex = dragIndex; 
    if(itemIndex==dragItem.Index) 
    { 
     return; 
    } 
    if(dragItem.Index<itemIndex) 
     itemIndex++; 
    else 
     itemIndex=dragIndex+i; 
    //Insert the item at the mouse pointer. 
    ListViewItem insertItem = (ListViewItem)dragItem.Clone(); 
    lvwFiles.Items.Insert(itemIndex, insertItem); 
    //Removes the item from the initial location while 
    //the item is moved to the new location. 
    lvwFiles.Items.Remove(dragItem); 
} 
+0

你问:“我该如何实现d + d的列表视图”,然后您发布的代码。这是一个真正的问题吗? – 2010-01-17 22:07:41

+0

代码接收目录中文件的路径,我在询问如何将ListView中的项从一个位置移动到另一个位置。是的,他们是相关的,但不一样。 – OIO 2010-01-17 23:16:11

+0

增加了一些信息。 – OIO 2010-01-18 16:02:48

回答

3

看一看ObjectListView - 围绕.NET的WinForms ListView的一个开放源代码的包装。

它支持通过拖动重新排列列表视图项目,再加上更多。见Taking the drag out of Drag and Drop

alt text http://objectlistview.sourceforge.net/cs/_images/dragdrop-dropbetween.png

+1

感谢您的链接。这个源文件有超过20个.cs文件,其中一个文件有9,000多行,我只想知道如何做到这一点,因为我已经将该事件用于其他目录中的D&D文件。我检查了文章中提到的属性和方法,但不明白,CanDrop方法只有一个命令:e.Effect = DragDropEffects.Move; – OIO 2010-01-18 15:12:16

+0

它确实做了很多,你是对的。在列表中拖动与从其他应用程序接受拖放完全不同。一旦将自动滚动和UI反馈考虑在内,它实际上非常复杂。 ObjectListView做了很多工作,以方便其他程序员使用它。 – Grammarian 2010-01-18 23:07:47

相关问题