【问题标题】:visual C# Change tab header colourvisual C# 更改选项卡标题颜色
【发布时间】:2016-06-05 10:47:42
【问题描述】:

我按照这个解决方案set-tabpage-header-color 更改了标签标题的颜色。但是,这会在选项卡控件中的所有选项卡的选项卡标题上设置相同的颜色。您能帮我只更改所选标签页眉的颜色吗? 非常感谢这里的任何帮助。谢谢

【问题讨论】:

    标签: c# colors tabs tabcontrol


    【解决方案1】:

    DrawItemEventArgs e 参数会告诉你所有你需要的。

    要以各种颜色绘制标题,请将 Brushes.Black 替换为 myBrush 并将 DrawString 放在这样的 using 子句中:

    using (SolidBrush myBrush = new SolidBrush (tabControl1.TabPages[e.Index].ForeColor))
    {
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, myBrush ,
                              e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, 
                              e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
    }
    

    现在每个标题都将在其TabPageForeColor 中绘制。

    DrawString 替换为TextRenderer.DrawText 会更好!

    如果您只想更改选定标签的颜色,只需使用如下检查:

    SolidBrush myBrush = new SolidBrush (e.State.HasFlag(DrawItemState.Selected) ? 
                         SystemColors.ActiveCaptionText : SystemColors.ControlText)
    

    【讨论】:

    • 谢谢!我意识到该方法是为选项卡控件中的每个选项卡调用的。我以为它只为选定的选项卡调用。现在它是有道理的。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多