• C# WinForm控件美化扩展系列之ListView


    昨天的文章中介绍了怎样使ListBox隔行显示不同的颜色,今天接着介绍怎样扩展ListView控件,使他也具有这样的功能,而且重绘他的Header,使其更美观。

    在这篇文章中,我只对View Details的时候进行了重绘,至于其他的Ttile,List,SmallIcon,LargeIcon,大家可以照着代码自己实现。

    这个控件的美化大致跟ListBox的差不了多少,我们还是一步一步的来实现它吧:

    1、  继承ListView,并把 OwnerDraw 设为True,如下:

           public ListViewEx()

                : base()

            {

                base.OwnerDraw = true;

    }

    2、  给继承的控件添加4个属性:RowBackColor1RowBackColor2SelectedColorHeadColor这三个颜色分别是数据项的交替的背景色、数据项选择后的背景色和列表头的背景颜色。

    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.Images[e.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,通过重写OnDrawItemOnDrawSubItem来实现,这里我只对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.Images[e.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 - imageRect.Right - 3,

                        bounds.Height);

                    TextRenderer.DrawText(

                        g,

                        e.Item.Text,

                        e.Item.Font,

                        textRect,

                        e.Item.ForeColor,

                        flags);

                    return;

                }

                e.DrawText(flags);

         }

     

    转载:http://www.csharpwin.net/ddwstp/net/csharp/418dr7279.shtml

     



    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


    随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

  • 相关阅读:
    linux的文件权限分析
    Bash 文件夹操作
    bash shell 文本文件操作
    Vim文字编辑
    Windows环境Vim编辑器如何执行Ruby代码
    JavaWeb-Servlet
    app遮罩层--网赚
    flex布局 居中
    实现绝对定位元素水平垂直居中的两种方法
    CSS背景图怎么自适应全屏(手机或者电脑)
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1990168.html
Copyright © 2020-2023  润新知