WinForm-ListBox控件美化

来源:互联网 发布:计算机处理数据的过程 编辑:程序博客网 时间:2024/05/16 01:11

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

属性
说明
Normal组件的所有元素都由操作系统绘制,并且元素大小都相等。OwnerDrawFixed组件的所有元素都是手动绘制的,并且元素大小都相等。OwnerDrawVariable组件的所有元素都由手动绘制,元素大小可能不相等。表01:枚举DrawMode中的成员及其说明

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

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

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

代码
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    int index = e.Index;//获取当前要进行绘制的行的序号,从0开始。
    Graphics g = e.Graphics;//获取Graphics对象。
    Rectangle bound = e.Bounds;//获取当前要绘制的行的一个矩形范围。
    string text = 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);
        Rectangle rect = new Rectangle(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 (Brush brush = 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 void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    //偶数行的ItemHeight为20
    if (e.Index % 2 == 0) e.ItemHeight = 20;
    //奇数行的ItemHeight为40
    else e.ItemHeight = 40;
}

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

原创粉丝点击