在ListView中更改列标题的字体样式和颜色 [英] Changing Font Style and Color for Column Header in ListView

查看:190
本文介绍了在ListView中更改列标题的字体样式和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了用于更改我们使用的ListView的页眉颜色的方法:

I searched that for changing Header color for a ListView we use:

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

我们使用相同的事件来更改ListView的页眉样式:

And we use the same event for changing Header Style for ListView:

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    using (StringFormat sf = new StringFormat())
    {
        sf.Alignment = StringAlignment.Center;
        e.DrawBackground();

        using (Font headerFont =
            new Font("Microsoft Sans Serif", 9, FontStyle.Bold)) //Font size!!!!
        {
            e.Graphics.DrawString(e.Header.Text, headerFont, 
                Brushes.Black, e.Bounds, sf);
        }
    }
}

现在我的问题是我想同时更改页眉颜色和页眉样式.所以我这样写:

Now my problem is I want to change both Header color as well as Header style. So I wrote like this:

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

    using (StringFormat sf = new StringFormat())
    {
        sf.Alignment = StringAlignment.Center;
        e.DrawBackground();

        using (Font headerFont =
            new Font("Microsoft Sans Serif", 9, FontStyle.Bold)) //Font size!!!!
        {
            e.Graphics.DrawString(e.Header.Text, headerFont,
                Brushes.Black, e.Bounds, sf);
        }
    }
}

但是,如果我执行此代码,则页眉将变为粗体,但页眉颜色不会更改.要同时更改它们(即标题颜色和标题样式),我要缺少什么?我不明白.

But if I execute this code, the Header is changing to Bold, but Header color is not changing. To change them both (i.e., Header color and Header Style) what am I missing? I do not understand.

推荐答案

尝试一下,即跳过对 e.DrawText() e.DrawBackground()的调用:

Try this, i.e. skip the call to e.DrawText() and e.DrawBackground():

private void list_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    using (var sf = new StringFormat())
    {
        sf.Alignment = StringAlignment.Center;

        using (var headerFont = new Font("Microsoft Sans Serif", 9, FontStyle.Bold))
        {
            e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
            e.Graphics.DrawString(e.Header.Text, headerFont, 
                Brushes.Black, e.Bounds, sf);
        }
    }
}

这篇关于在ListView中更改列标题的字体样式和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆