王朝网络
分享
 
 
 

带图标和自定义颜色的ListBox

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

自定义控件的实现

--------带图标和自定义颜色的ListBox

在一个点对点文件传输的项目中,我需要显示文件传输的实时信息:传输的文件列表和当前传输的文件,当时我想到了用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属性的类型

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

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

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 valid

if(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以完整的实现,并且支持可视化设计。

如果需要完整代码,请联系yzx110@bit.edu.cn

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有