簡體   English   中英

Winform C# Datagridview 繪制標題

[英]Winform C# Datagridview paint header

我想使用 c# 在 windows 窗體中自定義我的 datagridviews。 我想要做的是用我制作的漸變繪制數據網格標題:

public void Colorear_Barra_abajo(object sender, PaintEventArgs e)
    {
        Rectangle r = new Rectangle(0,0, panel_Borde_abajo.Width, panel_Borde_abajo.Height);

        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.FromArgb(255, 54, 54, 54);
            Color c2 = Color.FromArgb(255, 62, 62, 62);
            Color c3 = Color.FromArgb(255, 98, 98, 98);

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c3, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.5, 1 };
            cb.Colors = new[] { c1, c2 , c3 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }

問題是 datagridviews 沒有繪制事件,所以我不能使用它。 有沒有辦法用漸變繪制標題? 或者唯一的方法是選擇一種背景色?

謝謝..

如果您的意思是Column Header ,是的。 CellPainting Event

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex == -1)
        {
            Color c1 = Color.FromArgb(255, 54, 54, 54);
            Color c2 = Color.FromArgb(255, 62, 62, 62);
            Color c3 = Color.FromArgb(255, 98, 98, 98);

            LinearGradientBrush br = new LinearGradientBrush(e.CellBounds, c1, c3, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.5, 1 };
            cb.Colors = new[] { c1, c2, c3 };
            br.InterpolationColors = cb;


            e.Graphics.FillRectangle(br, e.CellBounds);
            e.PaintContent(e.ClipBounds);
            e.Handled = true;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM