C# ListView添加DragDrop

先建立好ListView,ImageList,
然后编写一个比较类
在就是添加DragDrop事件了
具体实现看代码吧

[csharp] view plain copy
 
    1. public partial class Form1 : Form  
    2. {  
    3.     public Form1()  
    4.     {  
    5.         InitializeComponent();  
    6.         InitializeListView();  
    7.     }  
    8.     // 初始化listView1.  
    9.     private void InitializeListView()  
    10.     {  
    11.         listView1.ListViewItemSorter = new ListViewIndexComparer();  
    12.         //初始化插入标记  
    13.         listView1.InsertionMark.Color = Color.Red;  
    14.         //  
    15.         listView1.AllowDrop = true;  
    16.     }  
    17.   
    18.     // 当一个项目拖拽是启动拖拽操作  
    19.     private void listView1_ItemDrag(object sender, ItemDragEventArgs e)  
    20.     {  
    21.         listView1.DoDragDrop(e.Item, DragDropEffects.Move);  
    22.     }  
    23.   
    24.     private void listView1_DragEnter(object sender, DragEventArgs e)  
    25.     {  
    26.         e.Effect = e.AllowedEffect;  
    27.     }  
    28.   
    29.     //像拖拽项目一样移动插入标记  
    30.     private void listView1_DragOver(object sender, DragEventArgs e)  
    31.     {  
    32.         // 获得鼠标坐标  
    33.         Point point = listView1.PointToClient(new Point(e.X, e.Y));  
    34.         // 返回离鼠标最近的项目的索引  
    35.         int index = listView1.InsertionMark.NearestIndex(point);  
    36.         // 确定光标不在拖拽项目上  
    37.         if (index > -1)  
    38.         {  
    39.             Rectangle itemBounds = listView1.GetItemRect(index);  
    40.             if (point.X > itemBounds.Left + (itemBounds.Width / 2))  
    41.             {  
    42.                 listView1.InsertionMark.AppearsAfterItem = true;  
    43.             }  
    44.             else  
    45.             {  
    46.                 listView1.InsertionMark.AppearsAfterItem = false;  
    47.             }  
    48.         }  
    49.         listView1.InsertionMark.Index = index;  
    50.     }  
    51.   
    52.     // 当鼠标离开控件时移除插入标记  
    53.     private void listView1_DragLeave(object sender, EventArgs e)  
    54.     {  
    55.         listView1.InsertionMark.Index = -1;  
    56.     }  
    57.   
    58.     // 将项目移到插入标记所在的位置  
    59.     private void listView1_DragDrop(object sender, DragEventArgs e)  
    60.     {  
    61.         // 返回插入标记的索引值  
    62.         int index = listView1.InsertionMark.Index;  
    63.         // 如果插入标记不可见,则退出.  
    64.         if (index == -1)  
    65.         {  
    66.             return;  
    67.         }  
    68.         // 如果插入标记在项目的右面,使目标索引值加一  
    69.         if (listView1.InsertionMark.AppearsAfterItem)  
    70.         {  
    71.             index++;  
    72.         }  
    73.   
    74.         // 返回拖拽项  
    75.         ListViewItem item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));  
    76.         //在目标索引位置插入一个拖拽项目的副本   
    77.         listView1.Items.Insert(index, (ListViewItem)item.Clone());  
    78.         // 移除拖拽项目的原文件  
    79.         listView1.Items.Remove(item);  
    80.     }  
    81.   
    82.     // 对ListView里的各项根据索引进行排序  
    83.     private class ListViewIndexComparer : System.Collections.IComparer  
    84.     {  
    85.         public int Compare(object x, object y)  
    86.         {  
    87.             return ((ListViewItem)x).Index - ((ListViewItem)y).Index;  
    88.         }  
    89.     }  
    90. }  
      1. http://blog.csdn.net/lilongherolilong/article/details/6689109
posted @ 2017-02-09 14:46  清空回声  阅读(409)  评论(0编辑  收藏  举报