您好,欢迎光临本网站![请登录][注册会员]  
文件名称: C# ListView 支持分组时拖拉排序的控件
  所属分类: C#
  开发工具:
  文件大小: 8kb
  下载次数: 0
  上传时间: 2009-04-03
  提 供 者: sn20****
 详细说明: using System; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; namespace WindowsFormsApplication1 { class DragItemListView:ListView { public DragItemListView() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); this.MultiSelect = false; this.ListViewItemSorter = new ListViewIndexComparer(); if (OSFeature.Feature.IsPresent(OS Feature.Themes)) { this.AllowDrop = true; this.ItemDrag += new ItemDragEventHandler(DragItemListView_ItemDrag); this.DragEnter += new DragEventHandler(DragItemListView_DragEnter); this.DragLeave += new EventHandler(DragItemListView_DragLeave); this.DragOver += new DragEventHandler(DragItemListView_DragOver); this.DragDrop += new DragEventHandler(DragItemListView_DragDrop); } } void DragItemListView_DragDrop(object sender, DragEventArgs e) { // Retrieve the index of the insertion mark; int targetIndex = this.InsertionMark.Index; // If the insertion mark is not visible, exit the method. if (targetIndex == -1) { return; } // If the insertion mark is to the right of the item with // the corresponding index, increment the target index. // Retrieve the dragged item. ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); // Insert a copy of the dragged item at the target index. // A copy must be inserted before the original item is removed // to preserve item index values. ListViewItem NewItem = (ListViewItem)draggedItem.Clone(); if (draggedItem.Index < targetIndex) { if (targetIndex - draggedItem.Index > 1) NewItem.Group = this.Items[targetIndex - 1].Group; else NewItem.Group = this.Items[targetIndex].Group; } else { if (draggedItem.Index - targetIndex > 1) NewItem.Group = this.Items[targetIndex].Group; else NewItem.Group = this.Items[targetIndex - 1].Group; } if (this.InsertionMark.AppearsAfterItem) { targetIndex++; } this.Items.Insert(targetIndex, NewItem); // Remove the original copy of the dragged item. this.Items.Remove(draggedItem); } void DragItemListView_DragOver(object sender, DragEventArgs e) { // Retrieve the client coordinates of the mouse pointer. Point targetPoint = this.PointToClient(new Point(e.X, e.Y)); // Retrieve the index of the item closest to the mouse pointer. //int targetIndex = this.InsertionMark.NearestIndex(targetPoint); int targetIndex = GetNearItem(targetPoint).Index; // Confirm that the mouse pointer is not over the dragged item. if (targetIndex > -1) { // Determine whether the mouse pointer is to the left or // the right of the midpoint of the closest item and set // the InsertionMark.AppearsAfterItem property accordingly. Rectangle itemBounds = this.GetItemRect(targetIndex); if (targetPoint.X > itemBounds.Left + (itemBounds.Width / 2)) { this.InsertionMark.AppearsAfterItem = true; } else { this.InsertionMark.AppearsAfterItem = false; } } // Set the location of the insertion mark. If the mouse is // over the dragged item, the targetIndex value is -1 and // the insertion mark disappears. this.InsertionMark.Index = targetIndex; } void DragItemListView_DragLeave(object sender, EventArgs e) { this.InsertionMark.Index = -1; } void DragItemListView_ItemDrag(object sender, ItemDragEventArgs e) { this.DoDragDrop(e.Item, DragDropEffects.Move); } void DragItemListView_DragEnter(object sender, DragEventArgs e) { e.Effect = e.AllowedEffect; } private class ListViewIndexComparer : System.Collections.IComparer { public int Compare(object x, object y) { return ((ListViewItem)x).Index - ((ListViewItem)y).Index; } } /// /// 搜索最近的ListViewItem /// /// 工作区坐标 /// public ListViewItem GetNearItem(Point ClientPoint) { ListViewItem lvt = this.GetItemAt(ClientPoint.X, ClientPoint.Y); if (lvt != null) return lvt; List vt = new List(); for (int i = 1; i < 10; i++) { lvt = this.GetItemAt(ClientPoint.X, ClientPoint.Y + i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X, ClientPoint.Y - i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X + i, ClientPoint.Y); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X + i, ClientPoint.Y + i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X + i, ClientPoint.Y - i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X - i, ClientPoint.Y); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X - i, ClientPoint.Y + i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X - i, ClientPoint.Y - i); if (lvt != null) vt.Add(lvt); if (vt.Count > 0) break; } if (vt.Count == 0) { return null; } else if (vt.Count == 1) { return vt[0]; } else { double HisDis = 0; int i = 0; foreach (ListViewItem item in vt) { double dis = GetDistince(ClientPoint, item.Position); if (i == 0 || dis < HisDis) { lvt = item; HisDis = dis; } i++; } return lvt; } } /// /// 两点间的距离 /// /// 起点 /// 终点 /// private double GetDistince(Point start, Point end) { double distince2 = Math.Pow((start.X - end.X), 2) + Math.Pow((start.Y - end.Y), 2); return Math.Abs(Math.Sqrt(distince2)); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: ListView 持分组 拖拉排序
 输入关键字,在本站1000多万海量源码库中尽情搜索: