开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 2143|回复: 3
收起左侧

[C#源码] 稍改进过的ListView

[复制链接]

结帖率:33% (1/3)
发表于 2013-2-22 02:02:00 | 显示全部楼层 |阅读模式   上海市上海市
改进了一点点的ListView,数据多不会闪。可以拖动行来进行排序了。
要启用拖放行,设置一下  AllowRecoder为true即可。另外多行选择=false
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using System.Collections;

  8. namespace Emao_ListView
  9. {
  10.     public class MyListView : System.Windows.Forms.ListView
  11.     {
  12.         #region Private Members

  13.         private ListViewItem m_previousItem;
  14.         private bool m_allowReorder;
  15.         private Color m_lineColor;

  16.         #endregion

  17.         #region Public Properties

  18.         [Category("Behavior")]
  19.         public bool AllowReorder
  20.         {
  21.             get { return m_allowReorder; }
  22.             set { m_allowReorder = value; }
  23.         }

  24.         [Category("Appearance")]
  25.         public Color LineColor
  26.         {
  27.             get { return m_lineColor; }
  28.             set { m_lineColor = value; }
  29.         }

  30.         #endregion

  31.         #region Protected and Public Methods

  32.         public MyListView()
  33.             : base()
  34.         {
  35.             this.FullRowSelect = true;
  36.             this.GridLines = true;
  37.             this.View = View.Details;
  38.             m_allowReorder = true;
  39.             m_lineColor = Color.Red;
  40.             SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer |
  41.                               System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
  42.                               System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,
  43.                               true);
  44.             UpdateStyles();
  45.         }
  46.         protected override void OnDragDrop(DragEventArgs drgevent)
  47.         {
  48.             if (!m_allowReorder)
  49.             {
  50.                 base.OnDragDrop(drgevent);
  51.                 return;
  52.             }

  53.             // get the currently hovered row that the items will be dragged to
  54.             Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
  55.             ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y);

  56.             if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()) || ((DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString())).ListView == null || ((DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString())).DragItems.Count == 0)
  57.                 return;

  58.             // retrieve the drag item data
  59.             DragItemData data = (DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString());

  60.             if (hoverItem == null)
  61.             {
  62.                 // the user does not wish to re-order the items, just append to the end
  63.                 for (int i = 0; i < data.DragItems.Count; i++)
  64.                 {
  65.                     ListViewItem newItem = (ListViewItem)data.DragItems[i];
  66.                     base.Items.Add(newItem);
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 // the user wishes to re-order the items

  72.                 // get the index of the hover item
  73.                 int hoverIndex = hoverItem.Index;

  74.                 // determine if the items to be dropped are from
  75.                 // this list view. If they are, perform a hack
  76.                 // to increment the hover index so that the items
  77.                 // get moved properly.
  78.                 if (this == data.ListView)
  79.                 {
  80.                     if (hoverIndex > base.SelectedItems[0].Index)
  81.                         hoverIndex++;
  82.                 }

  83.                 // insert the new items into the list view
  84.                 // by inserting the items reversely from the array list
  85.                 for (int i = data.DragItems.Count - 1; i >= 0; i--)
  86.                 {
  87.                     ListViewItem newItem = (ListViewItem)data.DragItems[i];
  88.                     base.Items.Insert(hoverIndex, newItem);
  89.                 }
  90.             }

  91.             // remove all the selected items from the previous list view
  92.             // if the list view was found
  93.             if (data.ListView != null)
  94.             {
  95.                 foreach (ListViewItem itemToRemove in data.ListView.SelectedItems)
  96.                 {
  97.                     data.ListView.Items.Remove(itemToRemove);
  98.                 }
  99.             }

  100.             // set the back color of the previous item, then nullify it
  101.             if (m_previousItem != null)
  102.             {
  103.                 m_previousItem = null;
  104.             }

  105.             this.Invalidate();

  106.             // call the base on drag drop to raise the event
  107.             base.OnDragDrop(drgevent);
  108.         }

  109.         protected override void OnDragOver(DragEventArgs drgevent)
  110.         {
  111.             if (!m_allowReorder)
  112.             {
  113.                 base.OnDragOver(drgevent);
  114.                 return;
  115.             }

  116.             if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
  117.             {
  118.                 // the item(s) being dragged do not have any data associated
  119.                 drgevent.Effect = DragDropEffects.None;
  120.                 return;
  121.             }

  122.             if (base.Items.Count > 0)
  123.             {
  124.                 // get the currently hovered row that the items will be dragged to
  125.                 Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
  126.                 ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y);

  127.                 Graphics g = this.CreateGraphics();

  128.                 if (hoverItem == null)
  129.                 {
  130.                     //MessageBox.Show(base.GetChildAtPoint(new Point(clientPoint.X, clientPoint.Y)).GetType().ToString());

  131.                     // no item was found, so no drop should take place
  132.                     drgevent.Effect = DragDropEffects.Move;

  133.                     if (m_previousItem != null)
  134.                     {
  135.                         m_previousItem = null;
  136.                         Invalidate();
  137.                     }

  138.                     hoverItem = base.Items[base.Items.Count - 1];

  139.                     if (this.View == View.Details || this.View == View.List)
  140.                     {
  141.                         g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
  142.                         g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5) });
  143.                         g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), new Point(this.Bounds.Width - 9, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5) });
  144.                     }
  145.                     else
  146.                     {
  147.                         g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
  148.                         g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - 5, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + 5, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + 5) });
  149.                         g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - 5, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + 5, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5) });
  150.                     }

  151.                     // call the base OnDragOver event
  152.                     base.OnDragOver(drgevent);

  153.                     return;
  154.                 }

  155.                 // determine if the user is currently hovering over a new
  156.                 // item. If so, set the previous item's back color back
  157.                 // to the default color.
  158.                 if ((m_previousItem != null && m_previousItem != hoverItem) || m_previousItem == null)
  159.                 {
  160.                     this.Invalidate();
  161.                 }

  162.                 // set the background color of the item being hovered
  163.                 // and assign the previous item to the item being hovered
  164.                 //hoverItem.BackColor = Color.Beige;
  165.                 m_previousItem = hoverItem;

  166.                 if (this.View == View.Details || this.View == View.List)
  167.                 {
  168.                     g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y));
  169.                     g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y - 5), new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 5) });
  170.                     g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y - 5), new Point(this.Bounds.Width - 9, hoverItem.Bounds.Y), new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + 5) });
  171.                 }
  172.                 else
  173.                 {
  174.                     g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
  175.                     g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - 5, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 5) });
  176.                     g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - 5, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5) });
  177.                 }

  178.                 // go through each of the selected items, and if any of the
  179.                 // selected items have the same index as the item being
  180.                 // hovered, disable dropping.
  181.                 foreach (ListViewItem itemToMove in base.SelectedItems)
  182.                 {
  183.                     if (itemToMove.Index == hoverItem.Index)
  184.                     {
  185.                         drgevent.Effect = DragDropEffects.None;
  186.                         hoverItem.EnsureVisible();
  187.                         return;
  188.                     }
  189.                 }

  190.                 // ensure that the hover item is visible
  191.                 hoverItem.EnsureVisible();
  192.             }

  193.             // everything is fine, allow the user to move the items
  194.             drgevent.Effect = DragDropEffects.Move;

  195.             // call the base OnDragOver event
  196.             base.OnDragOver(drgevent);
  197.         }

  198.         protected override void OnDragEnter(DragEventArgs drgevent)
  199.         {
  200.             if (!m_allowReorder)
  201.             {
  202.                 base.OnDragEnter(drgevent);
  203.                 return;
  204.             }

  205.             if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
  206.             {
  207.                 // the item(s) being dragged do not have any data associated
  208.                 drgevent.Effect = DragDropEffects.None;
  209.                 return;
  210.             }

  211.             // everything is fine, allow the user to move the items
  212.             drgevent.Effect = DragDropEffects.Move;

  213.             // call the base OnDragEnter event
  214.             base.OnDragEnter(drgevent);
  215.         }

  216.         protected override void OnItemDrag(ItemDragEventArgs e)
  217.         {
  218.             if (!m_allowReorder)
  219.             {
  220.                 base.OnItemDrag(e);
  221.                 return;
  222.             }

  223.             // call the DoDragDrop method
  224.             base.DoDragDrop(GetDataForDragDrop(), DragDropEffects.Move);

  225.             // call the base OnItemDrag event
  226.             base.OnItemDrag(e);
  227.         }

  228.         protected override void OnLostFocus(EventArgs e)
  229.         {
  230.             // reset the selected items background and remove the previous item
  231.             ResetOutOfRange();

  232.             Invalidate();

  233.             // call the OnLostFocus event
  234.             base.OnLostFocus(e);
  235.         }

  236.         protected override void OnDragLeave(EventArgs e)
  237.         {
  238.             // reset the selected items background and remove the previous item
  239.             ResetOutOfRange();

  240.             Invalidate();

  241.             // call the base OnDragLeave event
  242.             base.OnDragLeave(e);
  243.         }

  244.         #endregion
  245.         #region Private Methods

  246.         private DragItemData GetDataForDragDrop()
  247.         {
  248.             // create a drag item data object that will be used to pass along with the drag and drop
  249.             DragItemData data = new DragItemData(this);

  250.             // go through each of the selected items and
  251.             // add them to the drag items collection
  252.             // by creating a clone of the list item
  253.             foreach (ListViewItem item in this.SelectedItems)
  254.             {
  255.                 data.DragItems.Add(item.Clone());
  256.             }

  257.             return data;
  258.         }

  259.         private void ResetOutOfRange()
  260.         {
  261.             // determine if the previous item exists,
  262.             // if it does, reset the background and release
  263.             // the previous item
  264.             if (m_previousItem != null)
  265.             {
  266.                 m_previousItem = null;
  267.             }

  268.         }

  269.         #endregion

  270.         #region DragItemData Class

  271.         private class DragItemData
  272.         {
  273.             #region Private Members

  274.             private MyListView m_listView;
  275.             private ArrayList m_dragItems;

  276.             #endregion

  277.             #region Public Properties

  278.             public MyListView ListView
  279.             {
  280.                 get { return m_listView; }
  281.             }

  282.             public ArrayList DragItems
  283.             {
  284.                 get { return m_dragItems; }
  285.             }

  286.             #endregion

  287.             #region Public Methods and Implementation

  288.             public DragItemData(MyListView listView)
  289.             {
  290.                 m_listView = listView;
  291.                 m_dragItems = new ArrayList();
  292.             }

  293.             #endregion
  294.         }

  295.         #endregion
  296.     }
  297. }
复制代码
结帖率:94% (29/31)

签到天数: 5 天

发表于 2016-6-10 11:00:10 | 显示全部楼层   山东省青岛市
卡不懂。。。。。
回复 支持 反对

使用道具 举报

发表于 2013-5-1 18:45:32 | 显示全部楼层   广东省深圳市
牛逼啊 大婶不愧是大神啊
回复 支持 反对

使用道具 举报

发表于 2013-2-22 08:37:07 | 显示全部楼层   山东省烟台市
依然 支持 的一个下载点不容易
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表