如何使用colordialog控件更改ListView的列标题的颜色

时间:2015-01-05 17:49:37

标签: c# listview

我知道如何在运行时更改listview的标题颜色。即,在我写的代码中这样写:

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

由于我无法在此处放置屏幕截图,我只是想在这里解释一下。在WindowsForm中,我放置了ListView,ColorDialog和ContextMenuStrip。我为ListView添加了Header。现在,当用户右键单击ListView并选择颜色选项时运行应用程序时,将打开ColorDialog框。当用户选择一种颜色并按下确定按钮时,该颜色应应用于ListView的标题。这是我的要求。

我试过,但我没有得到任何相关的答案。所以我来到这里。任何答案将不胜感激。在此先感谢。

更改我的代码后:

private void BackColor_Click(object sender, EventArgs e)
    {
         DialogResult res=colorDialog1.ShowDialog();
         if (res==DialogResult.OK)
         {
              hdr=colorDialog1.Color;
              listView1.Update();
         }
    }

我将listView1_DrawColumnHeader事件更改为:

        using (Brush hBr=new SolidBrush(hdr))
        {
            e.Graphics.FillRectangle(hBr, e.Bounds);
            e.DrawText();
        }

但问题是,改变到给定的颜色需要将近2分钟。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

假设您将用户所需的颜色保存到Color var中,此处命名为hdrColor。使用它:

// form level var with a default:
Color hdrColor = SystemColors.Control;

private void _DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    using (Brush hBr = new SolidBrush(hdrColor))
    {
        e.Graphics.FillRectangle(hBr, e.Bounds);
        e.DrawText();
    }
}

在不破坏事件签名的情况下,您无法将其“传递”到事件中。因此,只需将所需颜色从颜色对话框保存到类级变量,从中创建画笔,然后在paint事件中使用它。请注意,using在完成后会处理画笔。

enter image description here enter image description here

相关问题