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

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

问题描述

问候,

我有一个标签控件,我想有标签1有它的文本颜色变化上的事件。
我发现像 C#的答案 - TabPage的颜色事件
和<一个href=\"http://stackoverflow.com/questions/1849801/c-winform-how-to-set-the-base-color-of-a-tabcontrol-not-the-tabpage\">C# Winform的:如何设置一个TabControl的基本颜色(不是Tab页)
但使用这些组的所有颜色,而不是一个。

所以我希望有一种方法用标签来实现这一点,我想更改为方法,而不是一个事件?

是这样的:

 公共无效SetTabPageHeaderColor(TabPage的页面,色色)
{
    //文本这里
}


解决方案

如果您想要的颜色的标签,请尝试以下code:

  this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem + =新System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);私人字典&LT; TabPage的,颜色和GT; TabColors =新词典&LT; TabPage的,颜色和GT;();
私人无效SetTabHeader(TabPage的页面,色色)
{
    TabColors [页码] =颜色;
    tabControl1.Invalidate();
}
私人无效tabControl1_DrawItem(对象发件人,DrawItemEventArgs E)
{
    //e.DrawBackground();
    使用(刷BR =新SolidBrush(TabColors [tabControl1.TabPages [e.Index]]))
    {
        e.Graphics.FillRectangle(BR,e.Bounds);
        SZ的SizeF = e.Graphics.MeasureString(tabControl1.TabPages [e.Index]。文本,e.Font);
        e.Graphics.DrawString(tabControl1.TabPages [e.Index]。文本,e.Font,Brushes.Black,e.Bounds.Left +(e.Bounds.Width - sz.Width)/ 2,e.Bounds.Top +(e.Bounds.Height - sz.Height)/ 2 + 1);        矩形RECT = e.Bounds;
        rect.Offset(0,1);
        rect.Inflate(0,-1);
        e.Graphics.DrawRectangle(Pens.DarkGray,RECT);
        e.DrawFocusRectangle();
    }
}

Greetings,

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?

Something like:

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天全站免登陆