设置 TabPage 标题颜色 [英] Set TabPage Header Color

查看:68
本文介绍了设置 TabPage 标题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

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

I have a tab control and I want to have 1 of the tabs have it's text color changed on a event. I've found answers like C# - TabPage Color event and C# Winform: How to set the Base Color of a TabControl (not the tabpage) but using these sets all colors instead of one.

所以我希望有一种方法可以使用我希望将其更改为方法而不是事件的选项卡来实现这一点?

So I was hoping there is a way to implement this with the tab I wish to change as a method instead of a event?

类似于:

public void SetTabPageHeaderColor(TabPage page, Color color) 
{
    //Text Here
}

推荐答案

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

If you want to color the tabs, try the following code:

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();
    }
}

这篇关于设置 TabPage 标题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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