C#中改变ListView中ColumnHeader的颜色

下面图是我改变后的效果,红色的是ListView的ColumnHeader:

    C#中改变ListView中ColumnHeader的颜色_第1张图片

    步骤1:

        将ListView的OwnerDraw属性改为True

 

    步骤2:

        建立ListView的DawColumnHeader消息函数

 

    步骤3:

        代码如下:

   private void lsvContactRcd_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
   {
        int tColumnCount;
        Rectangle tRect = new Rectangle();
        Point tPoint=new Point();

        Font tFont = new Font("宋体", 9, FontStyle.Regular);
        SolidBrush tBackBrush = new SolidBrush(System.Drawing.Color.FromArgb(((int)(((byte)(56)))), ((int)(((byte)(56)))), ((int)(((byte)(56))))));
        SolidBrush tFtontBrush;

        tFtontBrush = new SolidBrush (System.Drawing.SystemColors.GradientActiveCaption);

   

        if (lsvContactRcd.Columns.Count == 0)
                return;

 

        tColumnCount = lsvContactRcd.Columns.Count;
        tRect.Y = 0;
        tRect.Height = e.Bounds.Height - 1;
        tPoint.Y = 3;
        for (int i = 0; i < tColumnCount; i++)
        {
            if (i == 0)
            {
                tRect.X = 0;
                tRect.Width = lsvContactRcd.Columns[i].Width;
            }
            else
            {
                tRect.X += tRect.Width;
                tRect.X += 1;
                tRect.Width = lsvContactRcd.Columns[i].Width - 1;
            }

            e.Graphics.FillRectangle(tBackBrush, tRect);
            tPoint.X = tRect.X + 3;
            e.Graphics.DrawString(lsvContactRcd.Columns[i].Text, tFont, tFtontBrush, tPoint);
        }
    }

原文地址:http://blog.sina.com.cn/s/blog_4b650d650100o6ru.html

你可能感兴趣的:(C#,c#,listview,列表头)