【问题标题】:Set TabPage Header Color设置 TabPage 标题颜色
【发布时间】:2011-07-17 08:26:42
【问题描述】:

您好,

我有一个选项卡控件,我希望其中 1 个选项卡在事件中更改其文本颜色。 我找到了C# - TabPage Color event 之类的答案 和C# Winform: How to set the Base Color of a TabControl (not the tabpage) 但是使用这些设置所有颜色而不是一种。

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

类似:

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

【问题讨论】:

    标签: c# winforms colors header tabcontrol


    【解决方案1】:

    如果你想给标签上色,试试下面的代码:

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

    【讨论】:

    • 仍然基于事件。我想要一个像“SetTabHeader(TabPage page, Color color)”这样的方法
    • @Levisaxos,我已经添加了您需要的方法。但是您仍然需要该事件。
    • 很好的答案,但在 Windows 8 上效果不佳。将绘图模式设置为 OwnerDrawFixed 并添加绘图事件会使选项卡顶部的绘制风格与通常的风格大不相同。
    • 对于不在TabColors中的标签页,我们如何使用默认的DrawItem?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      添加 Fun Mun Pieng​​strong> 的答案,如果您要使用 垂直标签(如我是)那么你需要这样的东西:

          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

      特别感谢这个精彩的blog entry,它描述了如何在表单上进行文本旋转。

      【讨论】:

        【解决方案4】:
        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);
        
             }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-06
          • 2010-10-10
          • 1970-01-01
          • 2012-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多