winforms 更改Winform ListView标题的背景色

llmtgqce  于 2023-03-13  发布在  其他
关注(0)|答案(4)|浏览(355)

如何更改ListView标题的背景色?

nbewdwxp

nbewdwxp1#

可以通过将列表视图的OwnerDraw属性设置为true来执行此操作。
然后,这允许您为列表视图的绘制事件提供事件处理程序。
MSDN上有一个详细示例
下面是一些将标题颜色设置为红色的示例代码:

private void listView1_DrawColumnHeader(object sender,
                                            DrawListViewColumnHeaderEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.Red, e.Bounds);
        e.DrawText();
    }

我认为(但很高兴被证明是错误的),如果OwnerDraw设置为true,您还需要为其他具有默认实现的绘制事件提供处理程序,如下所示:

private void listView1_DrawItem(object sender,
                                    DrawListViewItemEventArgs e)
    {
        e.DrawDefault = true;
    }

我当然还没有设法使列表视图绘制的项目没有。

vpfxa7rd

vpfxa7rd2#

我知道现在说这个有点晚了,但是我还是看到了这个帖子,这对我有帮助。下面是大卫提供的代码的一个抽象应用

using System.Windows.Forms;
using System.Drawing;

//List view header formatters
public static void colorListViewHeader(ref ListView list, Color backColor, Color foreColor)
{
    list.OwnerDraw = true;
    list.DrawColumnHeader += 
        new DrawListViewColumnHeaderEventHandler
        (
            (sender, e) => headerDraw(sender, e, backColor, foreColor)
        );
    list.DrawItem += new DrawListViewItemEventHandler(bodyDraw);
}

private static void headerDraw(object sender, DrawListViewColumnHeaderEventArgs e, Color backColor, Color foreColor)
{
    using (SolidBrush backBrush = new SolidBrush(backColor))
    {
        e.Graphics.FillRectangle(backBrush, e.Bounds);
    }

    using (SolidBrush foreBrush = new SolidBrush(foreColor))
    {
        e.Graphics.DrawString(e.Header.Text, e.Font, foreBrush, e.Bounds);
    }
}

private static void bodyDraw(object sender, DrawListViewItemEventArgs e)
{
    e.DrawDefault = true;
}

然后在窗体构造函数中调用它

public Form()
{
    InitializeComponent();
    *CLASS NAME*.colorListViewHeader(ref myListView, *SOME COLOR*, *SOME COLOR*);
}

只需将 CLASS NAME 替换为您放入第一位代码的任何类,并将 SOME COLOR 替换为某种颜色。

//Some examples:
Color.white
SystemColors.ActiveCaption
Color.FromArgb(0, 102, 255, 102);
7xllpg7q

7xllpg7q3#

要更改标题的颜色并保持文本垂直居中,可以执行以下操作:
你可以像这样挂接listView的DrawColumnHeader:

listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);

则事件将如下所示:

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds); //Fill header with color

        //Adjust the position of the text to be vertically centered
        int yOffset = (e.Bounds.Height - e.Graphics.MeasureString(e.Header.Text, e.Font).ToSize().Height) / 2;
        Rectangle newBounds = new Rectangle(e.Bounds.X, e.Bounds.Y + yOffset, e.Bounds.Width, e.Bounds.Height - yOffset);

        e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, newBounds);
    }

让我知道它是否适合你!

bwleehnv

bwleehnv4#

现在是2023年,我不知道大卫的答案在2009年是否正确,但今天在屏幕上看起来真的很难看,它没有画出所有的文本,看起来可能只有第一个字母。
Nick Pray给出了一个更好的答案,但仍有一些问题需要考虑。首先,文本显示在右上角,因此您需要将其格式化为绘图的一部分。此外,每列之间没有显示线条,因此也需要设置。
根据Nick的例子,我做了一些修改,希望能有所帮助。

private void ListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        //Fills one solid background for each cell.
        using (SolidBrush backBrush = new SolidBrush(Color.FromArgb(6, 128, 128)))
        {
            e.Graphics.FillRectangle(backBrush, e.Bounds);
        }
        //Draw the borders for the header around each cell.
        using (Pen backBrush = new Pen(Color.Wheat))
        {
            e.Graphics.DrawRectangle(backBrush, e.Bounds);
        }
        using (SolidBrush foreBrush = new SolidBrush(Color.White))
        {
            //Since e.Header.TextAlign returns 'HorizontalAlignment' with values of (Right, Center, Left).  
            //DrawString uses 'StringAlignment' with values of (Near, Center, Far). 
            //We must translate these and setup a vertical alignment that doesn't exist in DrawListViewColumnHeaderEventArgs.
            StringFormat stringFormat = GetStringFormat(e.Header.TextAlign);

            //Do some padding, since these draws right up next to the border for Left/Near.  Will need to change this if you use Right/Far
            Rectangle rect = e.Bounds; rect.X += 2;
            e.Graphics.DrawString(e.Header.Text, e.Font, foreBrush, rect, stringFormat);
        }
    }

    private StringFormat GetStringFormat(HorizontalAlignment ha)
    {
        StringAlignment align;

        switch (ha)
        {
            case HorizontalAlignment.Right:
                align = StringAlignment.Far;
                break;
            case HorizontalAlignment.Center:
                align = StringAlignment.Center;
                break;
            default:
                align = StringAlignment.Near;
                break;
        }

        return new StringFormat()
        {
            Alignment = align,
            LineAlignment = StringAlignment.Center
        };
    }

相关问题