using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Collections; namespace Emao_ListView
{
public class MyListView : System.Windows.Forms.ListView
{
#region Private Members private ListViewItem m_previousItem;
private bool m_allowReorder;
private Color m_lineColor; #endregion #region Public Properties [Category("Behavior")]
public bool AllowReorder
{
get { return m_allowReorder; }
set { m_allowReorder = value; }
} [Category("Appearance")]
public Color LineColor
{
get { return m_lineColor; }
set { m_lineColor = value; }
} #endregion #region Protected and Public Methods public MyListView()
: base()
{
this.FullRowSelect = true;
this.GridLines = true;
this.View = View.Details;
m_allowReorder = true;
m_lineColor = Color.Red;
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer |
System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,
true);
UpdateStyles();
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragDrop(drgevent);
return;
} // get the currently hovered row that the items will be dragged to
Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y); 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 == )
return; // retrieve the drag item data
DragItemData data = (DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString()); if (hoverItem == null)
{
// the user does not wish to re-order the items, just append to the end
for (int i = ; i < data.DragItems.Count; i++)
{
ListViewItem newItem = (ListViewItem)data.DragItems[i];
base.Items.Add(newItem);
}
}
else
{
// the user wishes to re-order the items // get the index of the hover item
int hoverIndex = hoverItem.Index; // determine if the items to be dropped are from
// this list view. If they are, perform a hack
// to increment the hover index so that the items
// get moved properly.
if (this == data.ListView)
{
if (hoverIndex > base.SelectedItems[].Index)
hoverIndex++;
} // insert the new items into the list view
// by inserting the items reversely from the array list
for (int i = data.DragItems.Count - ; i >= ; i--)
{
ListViewItem newItem = (ListViewItem)data.DragItems[i];
base.Items.Insert(hoverIndex, newItem);
}
} // remove all the selected items from the previous list view
// if the list view was found
if (data.ListView != null)
{
foreach (ListViewItem itemToRemove in data.ListView.SelectedItems)
{
data.ListView.Items.Remove(itemToRemove);
}
} // set the back color of the previous item, then nullify it
if (m_previousItem != null)
{
m_previousItem = null;
} this.Invalidate(); // call the base on drag drop to raise the event
base.OnDragDrop(drgevent);
} protected override void OnDragOver(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragOver(drgevent);
return;
} if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
{
// the item(s) being dragged do not have any data associated
drgevent.Effect = DragDropEffects.None;
return;
} if (base.Items.Count > )
{
// get the currently hovered row that the items will be dragged to
Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y); Graphics g = this.CreateGraphics(); if (hoverItem == null)
{
//MessageBox.Show(base.GetChildAtPoint(new Point(clientPoint.X, clientPoint.Y)).GetType().ToString()); // no item was found, so no drop should take place
drgevent.Effect = DragDropEffects.Move; if (m_previousItem != null)
{
m_previousItem = null;
Invalidate();
} hoverItem = base.Items[base.Items.Count - ]; if (this.View == View.Details || this.View == View.List)
{
g.DrawLine(new Pen(m_lineColor, ), 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));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - ), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height - ), new Point(this.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(this.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height + ) });
}
else
{
g.DrawLine(new Pen(m_lineColor, ), 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));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height - ) });
} // call the base OnDragOver event
base.OnDragOver(drgevent); return;
} // determine if the user is currently hovering over a new
// item. If so, set the previous item's back color back
// to the default color.
if ((m_previousItem != null && m_previousItem != hoverItem) || m_previousItem == null)
{
this.Invalidate();
} // set the background color of the item being hovered
// and assign the previous item to the item being hovered
//hoverItem.BackColor = Color.Beige;
m_previousItem = hoverItem; if (this.View == View.Details || this.View == View.List)
{
g.DrawLine(new Pen(m_lineColor, ), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y - ), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - , hoverItem.Bounds.Y - ), new Point(this.Bounds.Width - , hoverItem.Bounds.Y), new Point(this.Bounds.Width - , hoverItem.Bounds.Y + ) });
}
else
{
g.DrawLine(new Pen(m_lineColor, ), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + ) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + , hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - ) });
} // go through each of the selected items, and if any of the
// selected items have the same index as the item being
// hovered, disable dropping.
foreach (ListViewItem itemToMove in base.SelectedItems)
{
if (itemToMove.Index == hoverItem.Index)
{
drgevent.Effect = DragDropEffects.None;
hoverItem.EnsureVisible();
return;
}
} // ensure that the hover item is visible
hoverItem.EnsureVisible();
} // everything is fine, allow the user to move the items
drgevent.Effect = DragDropEffects.Move; // call the base OnDragOver event
base.OnDragOver(drgevent);
} protected override void OnDragEnter(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragEnter(drgevent);
return;
} if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
{
// the item(s) being dragged do not have any data associated
drgevent.Effect = DragDropEffects.None;
return;
} // everything is fine, allow the user to move the items
drgevent.Effect = DragDropEffects.Move; // call the base OnDragEnter event
base.OnDragEnter(drgevent);
} protected override void OnItemDrag(ItemDragEventArgs e)
{
if (!m_allowReorder)
{
base.OnItemDrag(e);
return;
} // call the DoDragDrop method
base.DoDragDrop(GetDataForDragDrop(), DragDropEffects.Move); // call the base OnItemDrag event
base.OnItemDrag(e);
} protected override void OnLostFocus(EventArgs e)
{
// reset the selected items background and remove the previous item
ResetOutOfRange(); Invalidate(); // call the OnLostFocus event
base.OnLostFocus(e);
} protected override void OnDragLeave(EventArgs e)
{
// reset the selected items background and remove the previous item
ResetOutOfRange(); Invalidate(); // call the base OnDragLeave event
base.OnDragLeave(e);
} #endregion
#region Private Methods private DragItemData GetDataForDragDrop()
{
// create a drag item data object that will be used to pass along with the drag and drop
DragItemData data = new DragItemData(this); // go through each of the selected items and
// add them to the drag items collection
// by creating a clone of the list item
foreach (ListViewItem item in this.SelectedItems)
{
data.DragItems.Add(item.Clone());
} return data;
} private void ResetOutOfRange()
{
// determine if the previous item exists,
// if it does, reset the background and release
// the previous item
if (m_previousItem != null)
{
m_previousItem = null;
} } #endregion #region DragItemData Class private class DragItemData
{
#region Private Members private MyListView m_listView;
private ArrayList m_dragItems; #endregion #region Public Properties public MyListView ListView
{
get { return m_listView; }
} public ArrayList DragItems
{
get { return m_dragItems; }
} #endregion #region Public Methods and Implementation public DragItemData(MyListView listView)
{
m_listView = listView;
m_dragItems = new ArrayList();
} #endregion
} #endregion
}
}

改进了一点点的ListView,数据多不会闪。可以拖动行来进行排序了。 
以前主要是自己用。就这样了 
要启用拖放行,设置一下  AllowRecoder为true即可。另外多行选择=false

原文:http://www.oschina.net/code/snippet_191468_13278

【转】稍改进过的ListView,很好用哈的更多相关文章

  1. ListView 使用方法(Asp.Net)

    您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online  文件夹 L ...

  2. 【腾讯Bugly干货分享】跨平台 ListView 性能优化

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...

  3. CocosCreator之分层管理的ListView

    前言 进入公众号回复listview即可获得demo的git地址. 之前写的一篇文章<Creator之ScrollView那些事>中提到了官方Demo中提供的ListViewCtl,只是实 ...

  4. Listview详解

    Listview应该是最为常见的控件.对于大多数规则排列的界面,几乎都可以用ListView进行编写.对于单一界面来说,ListView既是最难的控件,又是使用最为频繁的控件.ListView 通常用 ...

  5. 改进duilib的richedit控件的部分功能

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 如果要使用透明异形窗体功能,首先要改进duilib库让他本 ...

  6. Android优化系列之ListView优化老生常谈

    本文内容:adapter,listview的优化,RecycleBi,google大会推荐优化, 实现ListView的过程,Adapter起到了至关重要的作用,不仅仅因为getview()方法.那么 ...

  7. android 一个页面内 多个listview的实现

    如果很平常的两个listview组件竖直放在linearLayout布局中,结果是: 两个listview 很独立,中间似乎有个分割线,完全吧他们分离了,各自独立滚动,如果上面的listview把整个 ...

  8. 【转】Android:ListView常见错位之CheckBox错位

    原文网址:http://blog.csdn.net/lemon_tree12138/article/details/39337867 ListView在什么样的情况下会出现错位?错位的原因是什么?怎么 ...

  9. ListView学习小结

    ListView小结 ListView 是Android UI中十分重要的一个组件,在数据的显示上能有着十分灵活的表现,其使用也比较简单,一般包括以下几个要点: 1.  可以通过编写ListActiv ...

随机推荐

  1. Excel和datatable相互操作

    /// <summary> /// Excel文档 /// </summary> /// <param name="table"></pa ...

  2. ajaxfileupload.js支持多文件上传【转载】

    //修改前代码------- //var oldElement = jQuery('#' + fileElementId); //var newElement = jQuery(oldElement) ...

  3. eclipse.ini

    -startup plugins/org.eclipse.equinox.launcher_1..jar --launcher.library plugins/org.eclipse.equinox. ...

  4. smarty 入门2(个人总结)

    1.下载安装: 2.拷贝libs文件夹到web文件夹: 3.引入smarty类文件   // include './libs/Smarty.class.php'; 4.配置smarty      // ...

  5. Eclipse序列号生成代码

    import java.io.*; public class MyEclipseGen { private static final String LL = "Decompiling thi ...

  6. mysql分区及实例演示

    一.为什么要分区? 需求:大数据.解决方案:分而治之,更细一点即为.将大表和大索引分为一个更小的操作单元 在mysql中,分区允许将表.索引和索引编排表细分为更小的单元.分区后,每个分区有自己单独的名 ...

  7. Git的常用操作

    $ git log //查看commit记录 $ git add <file> //添加文件到commit中 .代表所有改动的文件 $ git commit -m 'meesage' // ...

  8. python 常用模块 Top200

    名次 模块名称 被使用项目数 1 sys 7858 2 os 6983 3 re 5663 4 time 5268 5 random 3339 6 datetime 3310 7 setuptools ...

  9. django静态文件数据库设置

    STATIC_URL = '/static/'STATICFILES_DIRS = (        os.path.join(BASE_DIR,'static')) DATABASES = {    ...

  10. AngularJS Best Practices: SEO

    Google can execute AJAX & JavaScript for indexing, you can read the below link for more detailed ...