首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置TabPage标题颜色

设置TabPage标题颜色
EN

Stack Overflow用户
提问于 2011-03-17 19:49:00
回答 3查看 72.9K关注 0票数 29

大家好,

我有一个选项卡控件,我想有一个选项卡有它的文本颜色改变的事件。我已经找到了像C# - TabPage Color eventC# Winform: How to set the Base Color of a TabControl (not the tabpage)这样的答案,但是使用这些集合所有的颜色而不是一种颜色。

因此,我希望有一种方法来实现这一点,我希望将选项卡更改为方法而不是事件。

类似于:

代码语言:javascript
复制
public void SetTabPageHeaderColor(TabPage page, Color color) 
{
    //Text Here
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-17 19:53:03

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

代码语言:javascript
复制
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();
    }
}
票数 41
EN

Stack Overflow用户

发布于 2012-06-30 06:21:14

对于阅读本文的WinForms用户-只有在将选项卡控件的DrawMode设置为OwnerDrawFixed时才有效-如果设置为Normal,则DrawItem事件永远不会触发。

票数 21
EN

Stack Overflow用户

发布于 2020-06-20 19:57:47

代码语言:javascript
复制
private void MainForm_Load(object sender, EventArgs e)
{
       ...    
                
       this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
       this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
       ...
}


private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
     try
     {   
          // Draw the background of the control for each item.
          //e.DrawBackground();
            
          if (e.Index == this.tabControl1.SelectedIndex)
          {
              Brush _BackBrush = new SolidBrush(tabControl1.TabPages[e.Index].BackColor);
                   

              Rectangle rect = e.Bounds;
              e.Graphics.FillRectangle(_BackBrush, (rect.X) + 4, rect.Y, (rect.Width) - 4, rect.Height);
                    
              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);

         }
         else
         {   
              // 파스톤계 배경색 없앨려면 FromArgb 를 없애면 된다.
              Brush _BackBrush = new SolidBrush(Color.FromArgb(50, tabControl1.TabPages[e.Index].BackColor));

              Rectangle rect = e.Bounds;
              e.Graphics.FillRectangle(_BackBrush, rect.X, (rect.Y)-0, rect.Width, (rect.Height)+6);

              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 + 5);
                    
         }
            
     }
     catch (Exception Ex)
     {
          MessageBox.Show(Ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Information);

     }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5338587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档