设计带图标和自定义颜色的ListBox

2016-02-19 17:07 1 1 收藏

下面,图老师小编带您去了解一下设计带图标和自定义颜色的ListBox,生活就是不断的发现新事物,get新技能~

【 tulaoshi.com - 编程语言 】

  在一个点对点文件传输的项目中,我需要显示文件传输的实时信息:传输的文件列表和当前传输的文件,当时我想到了用ListBox,但是但我用了ListBox后,我发现它不能改变控件中文本想的颜色,于是我就想扩展一下ListBox控件------ListBoxEx。

  我的目标是给空间加上图标,还要能时时改变控件文本颜色。于是从ListBox派生类

  

public class ListBoxEx : ListBox {}

  为了操作方便我为ListBoxEx的每一项设计专门的类ListBoxExItem

  

public class ListBoxExItem {}

  为了保持我这个控件与WinForm的标准控件的操作借口一致,我又重新设计了两个集合类:

  

public class ListBoxExItemCollection : IList, ICollection, IEnumerator {}//这个类相对于标准ListBox中的ObjectCollection,这个类作为ListBoxEx中的Items属性的类型public class SelectedListBoxExItemCollection : : IList, ICollection, IEnumerator{}//这个类相对于标准ListBox中的SelectedObjectCollection,这个类作为ListBoxEx中的SelectedItems属性的类型

  下面看两个集合类的实现:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  ListBoxExItemCollection的实现:为了做到对集合(Items)的操作能够及时反映到ListBoxEx的控件中所以,此类只是对ListBox中Items(ObjectCollection类型)作了一层包装,就是把ListBox中Items属性的所有方法的只要是object类型的参数都转换成ListBoxExItem,比如:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

public void Remove(ListBoxExItem item){ this._Items.Remove(item); //_Items为ObjectCollection类型}public void Insert(int index, ListBoxExItem item){ this._Items.Insert(index, item);}public int Add(ListBoxExItem item){ return this._Items.Add(item);}

  由上可知,ListBoxExItemCollection中有一个构造函数来传递ListBox中的Items对象

  

private ObjectCollection _Items;public ListBoxExItemCollection(ObjectCollection baseItems){ this._Items = baseItems;}

  而SelectedListBoxExItemCollection类的实现也用同样的方法,只不过是对SelectedObjectCollection包装罢了。

  集合实现后,再来看ListBoxExItem的实现:

  为了使它支持图标和多种颜色添加如下成员

  

private int _ImageIndex;public int ImageIndex{ get { return this._ImageIndex; } set { this._ImageIndex = value;}}private Color _ForeColor;public Color ForeColor{ get{ return this._ForeColor;} set {  this._ForeColor = value;  this.Parent.Invalidate(); }}

  当然还有:

  

private string _Text;public string Text{ get { return this._Text; } set { this._Text = value; }}

  为了控件能正确显示此项的文本,还必须重写ToString()方法

  

public override string ToString(){ return this._Text;}

  再看ListBoxEx的实现:

  为了使控件能够自我绘制,所以:DrawMode = DrawMode.OwnerDrawFixed;

  为了覆盖基类的Items等相关属性添加

  

private ListBoxExItemCollection _Items; //在构造函数中创建

  同时还需要重写属性Items:

  

new public ListBoxExItemCollection Items{ get {  return this._Items; }}new public ListBoxExItem SelectedItem //强制转换为ListBoxExItem{ get{ return base.SelectedItem as ListBoxExItem;} set{ base.SelectedItem = value;}}new public SelectedListBoxExItemCollection SelectedItems //重新包装SelectedItems{ get {  return new SelectedListBoxExItemCollection(base.SelectedItems); }}

  为了支持图标,添加一个图像列表imagelist

  

private ImageList imageList;public ImageList ImageList{ get { return this.imageList; } set {  this.imageList = value;  this.Invalidate();//图像列表改变后马上更新控件 }}

  而此控件的核心却在一个方法OnDrawItem,这个方法每当控件的项需要重绘时就被调用

  

protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs pe){ pe.DrawBackground(); //画背景 pe.DrawFocusRectangle(); //画边框 Rectangle bounds = pe.Bounds; // Check whether the index is validif(pe.Index = 0 && pe.Index  base.Items.Count){ ListBoxExItem item = this.Items[pe.Index]; //取得需要绘制项的引用 int iOffset = 0;// If the image list is present and the image index is set, draw the image if(this.imageList != null) {  if (item.ImageIndex  -1 && item.ImageIndex  this.imageList.Images.Count)  {    this.imageList.Draw(pe.Graphics, bounds.Left, bounds.Top, bounds.Height, bounds.Height, item.ImageIndex); //绘制图标  }  iOffset += bounds.Height;//this.imageList.ImageSize.Width; } // Draw item text pe.Graphics.DrawString(item.Text, pe.Font, new SolidBrush(item.ForeColor),bounds.Left + iOffset, bounds.Top); //根据项的颜色绘制文本 } base.OnDrawItem(pe); }}

  到此为止,ListBoxEx以完整的实现,并且支持可视化设计。

来源:https://www.tulaoshi.com/n/20160219/1614234.html

延伸阅读
PowerPoint借助自定义形状实现填充自定义图片   具体如何操作呢?以powerpoint2007为例,先在PPT页面中画一Tulaoshi.Com个形状,就像下面这个圆: 然后在形状填充里面选择图片或纹理填充: 插入自文件,选择想要的图片就OK了,就会出现下面的效果: 同样的效果还可以应用于艺术字(文本框不行)。如下: ...
标签: flash教程
    通过前几篇教程,相信大家对于RGB颜色方面的编程应该已经是得心应手了吧?还有一个方面我们没有涉及,那就是:颜色叠加。等这一篇写完,我想我的这个RGB教程也该结束了,因为我肚子里可就只有这么点东西了。     这个色光叠加部分,更多的是一种想法,连算法都算不上。因为这一部分连可以copy了直接用的代...
标签: Web开发
一、基本概念: 1.标签(Tag): 标签是一种XML元素,通过标签可以使JSP网页变得简洁并且易于维护,还可以方便地实现同一个JSP文件支持多种语言版本。由于标签是XML元素,所以它的名称和属性都是大小写敏感的 2.标签库(Tag library): 由一系列功能相似、逻辑上互相联系的标签构成的集合称为标签库。 3.标签库描述文件(Tag Library Desc...
标签: windows10
Win10系统自定义主题颜色的方法   很多Win10系统电脑用户都想修改一下系统的主题颜色,因为Win10系统默认的主题颜色是:冷色调的淡蓝偏绿,很多喜欢暖色系的网友都想换一下主题。那么,Win10系统怎么自定义主题颜色呢?一起来看看今天的Win10系统使用教程吧! 颜色 关闭从我的背景自动选取一种主题色选项后,这里会出现一...

经验教程

38

收藏

78
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部