簡體   English   中英

設置 TabPage 標題顏色

[英]Set TabPage Header Color

問候,

我有一個選項卡控件,我希望其中一個選項卡在事件中更改其文本顏色。 我找到了類似C# - TabPage Color eventC# Winform: How to set Base Color of a TabControl (not the tabpage)但使用這些設置所有顏色而不是一種顏色的答案。

所以我希望有一種方法可以使用我希望將其更改為方法而不是事件的選項卡來實現這一點?

就像是:

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

對於閱讀此內容的 WinForms 用戶 - 僅當您將選項卡控件的 DrawMode 設置為 OwnerDrawFixed 時才有效 - 如果設置為 Normal,則 DrawItem 事件永遠不會觸發。

要添加到Fun Mun Pieng的答案,它在 Horizo​​ntal tabs上效果很好,如果你要使用Vertical tabs (就像我一樣),那么你需要這樣的東西:

    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

特別感謝這篇精彩的博客文章,它描述了如何在表單上旋轉文本。

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

     }
}

如果有人需要為選項卡標題添加顏色,請嘗試此操作。 我的標簽名稱 tabControl

tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += tabControl1_DrawItem;

然后在主類下清除它,

 private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                Color tabTextColor = Color.FromArgb(0x000001);
                var color = Color.FromArgb(tabTextColor.R, tabTextColor.G, tabTextColor.B); 
                TextRenderer.DrawText(e.Graphics, tabControl.TabPages[e.Index].Text, e.Font, e.Bounds, color);
            }

聲明這個函數它將生成輸出

最終出局

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM