介绍怎样扩展ListView控件.doc_第1页
介绍怎样扩展ListView控件.doc_第2页
介绍怎样扩展ListView控件.doc_第3页
介绍怎样扩展ListView控件.doc_第4页
介绍怎样扩展ListView控件.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

介绍怎样扩展ListView控件,使他也具有这样的功能,而且重绘他的Header,使其更美观。在这篇文章中,我只对View 为 Details的时候进行了重绘,至于其他的Ttile,List,SmallIcon,LargeIcon,大家可以照着代码自己实现。这个控件的美化大致跟ListBox的差不了多少,我们还是一步一步的来实现它吧:1、 继承ListView,并把 OwnerDraw 设为True,如下: public ListViewEx() : base() base.OwnerDraw = true;2、 给继承的控件添加4个属性:RowBackColor1,RowBackColor2,SelectedColor,HeadColor这三个颜色分别是数据项的交替的背景色、数据项选择后的背景色和列表头的背景颜色。3、 重绘Header,通过重写OnDrawColumnHeader函数来实现。 protected override void OnDrawColumnHeader( DrawListViewColumnHeaderEventArgs e) base.OnDrawColumnHeader(e); Graphics g = e.Graphics; Rectangle bounds = e.Bounds; Color baseColor = _headColor; Color borderColor = _headColor; Color innerBorderColor = Color.FromArgb(200, 255, 255); RenderBackgroundInternal( g, bounds, baseColor, borderColor, innerBorderColor, 0.35f, true, LinearGradientMode.Vertical); TextFormatFlags flags = GetFormatFlags(e.Header.TextAlign); Rectangle textRect = new Rectangle( bounds.X + 3, bounds.Y, bounds.Width - 6, bounds.Height); ; if (e.Header.ImageList != null) Image image = e.Header.ImageIndex = -1 ? null : e.Header.ImageList.Imagese.Header.ImageIndex; if (image != null) Rectangle imageRect = new Rectangle( bounds.X + 3, bounds.Y + 2, bounds.Height - 4, bounds.Height - 4); g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImage(image, imageRect); textRect.X = imageRect.Right + 3; textRect.Width -= imageRect.Width; TextRenderer.DrawText( g, e.Header.Text, e.Font, textRect, e.ForeColor, flags); 4、 重绘 Item,通过重写OnDrawItem,OnDrawSubItem来实现,这里我只对Details这种显示模式进行了重写。 protected override void OnDrawItem(DrawListViewItemEventArgs e) base.OnDrawItem(e); if (View != View.Details) e.DrawDefault = true; protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) base.OnDrawSubItem(e); if (View != View.Details) return; if(e.ItemIndex = -1) return; Rectangle bounds = e.Bounds; ListViewItemStates itemState = e.ItemState; Graphics g = e.Graphics; if (itemState & ListViewItemStates.Selected) = ListViewItemStates.Selected) bounds.Height-; Color baseColor = _selectedColor; Color borderColor = _selectedColor; Color innerBorderColor = Color.FromArgb(200, 255, 255); RenderBackgroundInternal( g, bounds, baseColor, borderColor, innerBorderColor, 0.35f, true, LinearGradientMode.Vertical); bounds.Height+; else Color backColor = e.ItemIndex % 2 = 0 ? _rowBackColor1 : _rowBackColor2; using (SolidBrush brush = new SolidBrush(backColor) g.FillRectangle(brush, bounds); TextFormatFlags flags = GetFormatFlags(e.Header.TextAlign); if (e.ColumnIndex = 0) if (e.Item.ImageList = null) e.DrawText(flags); return; Image image = e.Item.ImageIndex = -1 ? null : e.Item.ImageList.Imagese.Item.ImageIndex; if (image = null) e.DrawText(flags); return; Rectangle imageRect = new Rectangle( bounds.X + 4, bounds.Y + 2, bounds.Height - 4, bounds.Height - 4); g.DrawImage(image, imageRect); Rectangle textRect = new Rectangle( imageRect.Right + 3, bounds.Y, bounds.Width -

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论