【问题标题】:How can I change color of column Header for a ListView using colordialog control如何使用 colordialog 控件更改 ListView 的列标题颜色
【发布时间】:2015-01-06 02:59:06
【问题描述】:

我知道如何在运行前更改列表视图的标题颜色。即,在我这样写的代码中:

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

由于我无法将屏幕截图放在这里,我只是想在这里解释一下。在 WindowsForm 中,我放置了 ListView、ColorDialog 和 ContextMenuStrip。我为 ListView 添加了标题。现在,当用户右键单击 ListView 并选择颜色选项时运行应用程序时,将打开颜色对话框。当用户选择一种颜色并按下确定按钮时,该颜色应应用于 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 分钟才能更改为给定的颜色。你能帮我吗?

【问题讨论】:

  • 好吧,如果涉及颜色对话框,则在运行时选择颜色。将颜色保存到 var 并在代码中使用。
  • @Plutonix 好的,我保存在 var.但是我们如何将该变量传递给 listView1_DrawColumnHeader 事件呢?我是初学者,请帮帮我。
  • 更新您的帖子以显示您所做/尝试过的事情
  • @Plutonix 你能帮帮我吗,我更新了我的需求
  • 当这种情况发生时,LV 中有多少项目?它仅适用于零个或 1 个项目吗?作为 OwnerDraw,您还必须处理DrawItem,也许瓶颈就在那里。

标签: c# listview


【解决方案1】:

假设您将用户所需的颜色保存到 Color 变量中,这里命名为 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();
    }
}

您不能在不破坏事件签名的情况下将其“传递”给事件。因此,只需将颜色对话框中的所需颜色保存到类级别变量中,从中创建一个画笔,然后在绘制事件中使用它。请注意,using 会在我们使用完画笔后将其丢弃。

【讨论】:

  • 现在我像这样更改了我的代码 private void headerBackColorToolStripMenuItem1_Click(object sender, EventArgs e) { DialogResult result = colorDialog1.ShowDialog(); if (result == DialogResult.OK) { hdr = colorDialog1.Color; listView1.Update();在我的 listView1_DrawColumnHeader 事件中,我更改为: using (Brush hBr = new SolidBrush(hdr)) { e.Graphics.FillRectangle(hBr, e.Bounds); }
  • 代码在 cmets 上没有格式,我看不懂。 Edit your post 更新/更改您的问题
猜你喜欢
  • 1970-01-01
  • 2014-05-17
  • 2015-04-30
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多