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. PowerDesigner 15.2入门学习 一

    好久没有搞 PowerDesigner 然后记录一下 1.下载地址 http://download.sybase.com/eval/PowerDesigner/PowerDesigner152_Eva ...

  2. Thinking in Java——笔记(5)

    Initialization & Cleanup Guaranteed initialization with the constructor In Java, the class desig ...

  3. iOS简易图片选择器 (图片可多选,仿微信)

    调用方法 NickyImagePickerViewController *pickerController = [[NickyImagePickerViewController alloc]init] ...

  4. 经典C#编程理解,概要,经典

    一.NET框架 ADO.NET微软提供的一组类库,可以帮助程序员和数据库交互. CLS(公共语言规范) CTS(通用语言类型) 类库: 可以看成一个承载了N个类的容器. 类库和命名空间: 一个类库对应 ...

  5. 周记:Linux下C编程

    也啥都没干,计划一个都没干.吼吼-- 周六去看[21天学通Linux_C编程]笔记: 突然都对这本书没信心了.刚开始,就出现的不满如下:1.创建vim启动器时候,类型要选择[终端下的应用程序]没说,虽 ...

  6. Maven-008-Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 解决方案

    我在部署构件至 maven nexus 私服时,有时会出现 Failed to deploy artifacts: Failed to transfer file: ... Return code i ...

  7. JAVASE02-Unit02: 正则表达式 、 Object 、 包装类

    正则表达式 . Object . 包装类 字符串支持正则表达式的方法一: package day02; /** * 字符串支持正则表达式的方法一: * boolean matches(String r ...

  8. CentOS-7.0.中安装与配置Tomcat-7的方法

    安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:http://tomcat.apache.org/down ...

  9. iOS UITableView 移除单元格选中时的高亮状态

    在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...

  10. SpringMVC源码剖析(一)- 从抽象和接口说起

    SpringMVC作为Struts2之后异军突起的一个表现层框架,正越来越流行,相信javaee的开发者们就算没使用过SpringMVC,也应该对其略有耳闻.我试图通过对SpringMVC的设计思想和 ...