700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > WinForm-ListBox控件美化

WinForm-ListBox控件美化

时间:2023-11-14 05:30:35

相关推荐

WinForm-ListBox控件美化

这是本人原创的Listbox美化效果

/mosangbike/article/details/84340615

以下是转发文章:

WinForm-ListBox控件美化

(-08-25 22:52:05)

转载▼

如果要对ListBox控件进行自定义绘制(美化),那么首先必须将ListBox的DrawMode属性设置为OwnerDrawVariable或OwnerDrawFixed。ListBox有个ItemHeight属性,在DrawMode设置为Normal时,这个属性是不可设置的,并且其值是根据当前字体进行计算获得的。只有当DrawMode设置为OwnerDrawVariable或OwnerDrawFixed时,设置ItemHeight才生效。

表01:枚举DrawMode中的成员及其说明

设置完DrawMode属性之后,通过ListBox的DrawItem事件可以绘制自己想要的个性化控件。先看一下自己绘制的ListBox控件的效果图:

(这是选中“英语”的效果)

从图中可以看出,针对不同的行绘制了不同的背景色,选中项的背景色设置为蓝色,并且还绘制了一个边框。确实比系统绘制的ListBox好看多了。下面我们来看看代码,也就是DrawItem事件处理方法。

代码

private voidlistBox1_DrawItem(objectsender,DrawItemEventArgse)

{

intindex = e.Index;//获取当前要进行绘制的行的序号,从0开始。

Graphicsg = e.Graphics;//获取Graphics对象。

Rectanglebound = e.Bounds;//获取当前要绘制的行的一个矩形范围。

stringtext = listBox1.Items[index].ToString();//获取当前要绘制的行的显示文本。

if((e.State &DrawItemState.Selected) ==DrawItemState.Selected)

{//如果当前行为选中行。

//绘制选中时要显示的蓝色边框。

g.DrawRectangle(Pens.Blue, bound.Left, bound.Top, bound.Width - 1, bound.Height - 1);

Rectanglerect = newRectangle(bound.Left 2, bound.Top 2,

bound.Width - 4, bound.Height - 4);

//绘制选中时要显示的蓝色背景。

g.FillRectangle(Brushes.Blue, rect);

//绘制显示文本。

TextRenderer.DrawText(g, text,this.Font, rect,Color.White,

TextFormatFlags.VerticalCenter |TextFormatFlags.Left);

}

else

{//GetBrush为自定义方法,根据当前的行号来选择Brush进行绘制。

using(Brushbrush = GetBrush(e.Index))

{

g.FillRectangle(brush, bound);//绘制背景色。

}

TextRenderer.DrawText(g, text,this.Font, bound,Color.White,

TextFormatFlags.VerticalCenter |TextFormatFlags.Left);

}

}

OwnerDrawVariable

设置DrawMode属性为OwnerDrawVariable后,可以任意改变每一行的ItemHeight和ItemWidth。通过ListBox的MeasureItem事件,可以使每一行具有不同的大小。

(奇偶行的行高不同)

private voidlistBox1_MeasureItem(objectsender,MeasureItemEventArgse)

{

//偶数行的ItemHeight为20

if(e.Index % 2 == 0) e.ItemHeight = 20;

//奇数行的ItemHeight为40

elsee.ItemHeight = 40;

}

总结

这里最重要的是DrawItem事件和MeasureItem事件,以及MeasureItemEventArgs事件数据类和DrawItemEventArgs事件数据类。在System.Windows.Forms命名空间中,具有DrawItem事件的控件有ComboBox、ListBox、ListView、MenuItem、StatusBar、TabControl,具有MeasureItem事件的控件有ComboBox、ListBox、MenuItem。所以,这些控件可以采用和ListBox相同的方法进行自定义绘制。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。