C# 设置选项卡页标题颜色

C# 设置选项卡页标题颜色,c#,winforms,colors,header,tabcontrol,C#,Winforms,Colors,Header,Tabcontrol,您好 我有一个选项卡控件,我想让其中一个选项卡在事件中更改其文本颜色。 我找到了这样的答案 及 但是使用这些设置所有颜色而不是一种 所以我希望有一种方法可以通过我希望作为方法而不是事件更改的选项卡来实现这一点 比如: public void SetTabPageHeaderColor(TabPage page, Color color) { //Text Here } 如果要为选项卡着色,请尝试以下代码: this.tabControl1.DrawMode = TabDrawMode

您好

我有一个选项卡控件,我想让其中一个选项卡在事件中更改其文本颜色。 我找到了这样的答案 及 但是使用这些设置所有颜色而不是一种

所以我希望有一种方法可以通过我希望作为方法而不是事件更改的选项卡来实现这一点

比如:

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

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

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();
    }
}
this.tabControl1.DrawMode=TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem+=新的System.Windows.Forms.DrawItemEventHandler(this.tabControl1\u DrawItem);
私有字典TabColors=新字典();
私有void SetTabHeader(选项卡页面,颜色)
{
TabColors[页面]=颜色;
tabControl1.Invalidate();
}
私有无效选项卡Control1\u DrawItem(对象发送方,DrawItemEventArgs e)
{
//e、 牵引杆接地();
使用(画笔br=new SolidBrush(TabColors[tabControl1.TabPages[e.Index]]))
{
e、 图形填充矩形(br,e.Bounds);
SizeF sz=e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text,e.Font);
e、 Graphics.DrawString(tabControl1.TabPages[e.Index]。文本,e.Font,画笔。黑色,e.Bounds.Left+(e.Bounds.Width-sz.Width)/2,e.Bounds.Top+(e.Bounds.Height-sz.Height)/2+1);
矩形矩形矩形=e.边界;
直线偏移量(0,1);
直线充气(0,-1);
e、 图形.DrawRectangle(Pens.DarkGray,rect);
e、 DrawFocusRectangle();
}
}

对于阅读此内容的WinForms用户-只有当您将选项卡控件的DrawMode设置为OwnerDrawFixed时,此选项才有效-如果设置为Normal,DrawItem事件将不会触发。

要添加到Fun Mun Pieng的答案中,如果您使用垂直选项卡(就像我一样),该答案在水平选项卡上工作得很好那么你需要这样的东西:

    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);
        }
    }
我将重复ROJO1969提出的观点,如果这是在WinForms中,那么您必须将DrawMode设置为OwnerDrawFixed


特别感谢这篇精彩的文章,它描述了如何在表单上旋转文本。

仍然基于一个事件。我想要一个像“SetTabHeader(TabPage页面,Color)”这样的方法@Levisos,我已经添加了您需要的方法。但是你仍然需要这个事件。答案很好,但是它在Windows8上不太好用。将绘图模式设置为OwnerDrawFixed并添加绘图事件会使选项卡顶部的绘图样式与通常的绘图样式大不相同。对于不使用TabColor的选项卡页面,如何使用默认的DrawItem?
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);

     }
}