2

我正在使用 Winform datagridview 并尝试通过更改背景色来自定义我的标题。为了能够修改颜色,我必须将EnableHeadersVisualStyles属性设置为 false。

但是当我设置这个属性时,我失去了默认的 3D 主题。您知道是否有任何解决方法可以保存 3D 样式并具有彩色标题?

我的代码:

dataGridTaxes.EnableHeadersVisualStyles = false;
dataGridTaxes.ColumnHeadersDefaultCellStyle = BlueDataGridHeaderStyle.GetInstance();

其中 BlueDataGridHeaderStyle 只是具有新 BackColor 的基本样式

4

1 回答 1

5

您必须自定义您的DataGridView,这是我为您制作的示例代码。与默认的 3D 样式相比,它看起来非常好,ColumnHeader但支持BackColorHover backcolor

    //CellPainting event handler for your dataGridView1
    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
        if (e.RowIndex == -1 && e.ColumnIndex > -1){
            e.PaintBackground(e.CellBounds, true);
            RenderColumnHeader(e.Graphics, e.CellBounds, e.CellBounds.Contains(hotSpot) ? hotSpotColor : backColor);
            RenderColumnHeaderBorder(e.Graphics, e.CellBounds, e.ColumnIndex);
            using (Brush brush = new SolidBrush(e.CellStyle.ForeColor)){
                using (StringFormat sf = new StringFormat() {LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center}) {
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brush, e.CellBounds, sf);                        
                }
            }
            e.Handled = true;
        }
    }
    Color hotSpotColor = Color.LightGreen;//For hover backcolor
    Color backColor = Color.LimeGreen;    //For backColor    
    Point hotSpot;
    private void RenderColumnHeader(Graphics g, Rectangle headerBounds, Color c) {
        int topHeight = 10;
        Rectangle topRect = new Rectangle(headerBounds.Left, headerBounds.Top+1, headerBounds.Width, topHeight);
        RectangleF bottomRect = new RectangleF(headerBounds.Left, headerBounds.Top+1 + topHeight, headerBounds.Width, headerBounds.Height- topHeight-4);            
        Color c1 = Color.FromArgb(180, c);            
        using (SolidBrush brush = new SolidBrush(c1)) {
            g.FillRectangle(brush, topRect);
            brush.Color = c;
            g.FillRectangle(brush, bottomRect);
        }
    }
    private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex) {            
        g.DrawRectangle(new Pen(Color.White, 0.1f), headerBounds.Left + 0.5f, headerBounds.Top + 0.5f,headerBounds.Width-1f,headerBounds.Height-1f);
        ControlPaint.DrawBorder(g, headerBounds, Color.Gray, 0, ButtonBorderStyle.Inset,
                                               Color.Gray, 0, ButtonBorderStyle.Inset,
                                             Color.Gray, colIndex != dataGridView1.ColumnCount - 1 ? 1 : 0, ButtonBorderStyle.Inset,
                                           Color.Gray, 1, ButtonBorderStyle.Inset);
    }
    //MouseMove event handler for your dataGridView1
    private void dataGridView1_MouseMove(object sender, MouseEventArgs e){
        hotSpot = e.Location;
    }
    //MouseLeave event handler for your dataGridView1
    private void dataGridView1_MouseLeave(object sender, EventArgs e){
        hotSpot = Point.Empty;
    }

在此处输入图像描述

您可以RenderColumnHeaderBorder使用以下方式实现ControlPaint.DrawBorder3D,它更简单:

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
    ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.Raised, Border3DSide.All & ~Border3DSide.Middle);
}

在此处输入图像描述

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
    ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.RaiseInner, Border3DSide.All & ~Border3DSide.Middle);
}

在此处输入图像描述

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
    ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.RaiseOuter, Border3DSide.All & ~Border3DSide.Middle);
}

在此处输入图像描述

于 2013-10-10T14:10:14.087 回答