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

下面是自己的心得与补充

上文中调用的函数RenderBackgroundInternal具体代码如下:

 

public static void RenderBackgroundInternal(Graphics g, Rectangle rect,
        Color baseColor, Color borderColor, Color innerBorderColor,
        float basePosition, bool drawBorder, LinearGradientMode mode)
        {
            if (drawBorder)
            {
                rect.Width--;
                rect.Height--;
            }
            using (LinearGradientBrush brush = new LinearGradientBrush(
                rect, Color.Transparent, Color.Transparent, mode))
            {
                Color[] colors = new Color[4];
                colors[0] = GetColor(baseColor, 0, 35, 24, 9);
                colors[1] = GetColor(baseColor, 0, 13, 8, 3);
                colors[2] = baseColor;
                colors[3] = GetColor(baseColor, 0, 68, 69, 54);

 

                ColorBlend blend = new ColorBlend();
                blend.Positions = new float[] { 0.0f, basePosition, basePosition + 0.05f, 1.0f };
                blend.Colors = colors;
                brush.InterpolationColors = blend;
                g.FillRectangle(brush, rect);
            }
            if (baseColor.A > 80)
            {
                Rectangle rectTop = rect;
                if (mode == LinearGradientMode.Vertical)
                {
                    rectTop.Height = (int)(rectTop.Height * basePosition);
                }
                else
                {
                    rectTop.Width = (int)(rect.Width * basePosition);
                }
                using (SolidBrush brushAlpha =
                    new SolidBrush(Color.FromArgb(80, 255, 255, 255)))
                {
                    g.FillRectangle(brushAlpha, rectTop);
                }
            }
            if (drawBorder)
            {
                using (Pen pen = new Pen(borderColor))
                {
                    g.DrawRectangle(pen, rect);
                }

 

                rect.Inflate(-1, -1);
                using (Pen pen = new Pen(innerBorderColor))
                {
                    g.DrawRectangle(pen, rect);
                }
            }
        }
        private static Color GetColor(Color colorBase, int a, int r, int g, int b)
        {
            int a0 = colorBase.A;
            int r0 = colorBase.R;
            int g0 = colorBase.G;
            int b0 = colorBase.B;

 

            if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); }
            if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); }
            if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); }
            if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); }

 

            return Color.FromArgb(a, r, g, b);
        }

上文中,函数GetFormatFlags(e.Header.TextAlign);不知道到底是啥,自己改写成TextFormatFlags.xxx(XXX按照自己需求填写);

 

posted on 2013-04-16 12:00  泼墨水月  阅读(3568)  评论(0编辑  收藏  举报