设置TabPage标题颜色

The*_*der 27 c# tabcontrol header colors winforms

问候,

我有一个标签控件,我希望其中一个标签在事件中更改了文本颜色.我找到了像C# - TabPage Color事件C#Winform这样的答案:如何设置TabControl的基色(不是标签页), 但使用这些设置所有颜色而不是一个.

所以我希望有一种方法可以通过我想改变的方法来实现这个方法而不是事件?

就像是:

public void SetTabPageHeaderColor(TabPage page, Color color) 
{
    //Text Here
}
Run Code Online (Sandbox Code Playgroud)

Fun*_*eng 33

如果要为选项卡着色,请尝试以下代码:

this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);

private Dictionary<TabPage, Color> TabColors = new Dictionary<TabPage, Color>();
private void SetTabHeader(TabPage page, Color color)
{
    TabColors[page] = color;
    tabControl1.Invalidate();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    //e.DrawBackground();
    using (Brush br = new SolidBrush (TabColors[tabControl1.TabPages[e.Index]]))
    {
        e.Graphics.FillRectangle(br, e.Bounds);
        SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);

        Rectangle rect = e.Bounds;
        rect.Offset(0, 1);
        rect.Inflate(0, -1);
        e.Graphics.DrawRectangle(Pens.DarkGray, rect);
        e.DrawFocusRectangle();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案,但它在 Windows 8 上效果不佳。将绘图模式设置为 OwnerDrawFixed 并添加绘图事件会使选项卡顶部的绘制风格与通常的风格大不相同。 (2认同)

小智 17

对于阅读此内容的WinForms用户 - 仅当您将选项卡控件的DrawMode设置为OwnerDrawFixed时,此方法才有效 - 如果将DrawItem事件设置为"正常",则它永远不会触发.


Der*_*k W 7

要添加到Fun Mun Pieng的答案,它在Horizo​​ntal选项卡上运行得很漂亮,如果你要使用Vertical tabs(就像我一样)那么你需要这样的东西:

    private void tabControl2_DrawItem(object sender, DrawItemEventArgs e)
    {
        using (Brush br = new SolidBrush(tabColorDictionary[tabControl2.TabPages[e.Index]]))
        {
            // Color the Tab Header
            e.Graphics.FillRectangle(br, e.Bounds);
            // swap our height and width dimensions
            var rotatedRectangle = new Rectangle(0, 0, e.Bounds.Height, e.Bounds.Width);

            // Rotate
            e.Graphics.ResetTransform();
            e.Graphics.RotateTransform(-90);

            // Translate to move the rectangle to the correct position.
            e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Bottom, System.Drawing.Drawing2D.MatrixOrder.Append);

            // Format String
            var drawFormat = new System.Drawing.StringFormat();
            drawFormat.Alignment = StringAlignment.Center;
            drawFormat.LineAlignment = StringAlignment.Center;

            // Draw Header Text
            e.Graphics.DrawString(tabControl2.TabPages[e.Index].Text, e.Font, Brushes.Black, rotatedRectangle, drawFormat);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我将回应ROJO1969所做的一点,如果这是在WinForms中 - 那么你必须将DrawMode设置为OwnerDrawFixed.

特别感谢这篇精彩的博客文章,其中描述了如何在表单上轮换文本.