winforms 设置选项卡页眉颜色

z9ju0rcb  于 2023-04-12  发布在  其他
关注(0)|答案(6)|浏览(150)

您好
我有一个标签控件,我想有一个标签有它的文本颜色改变了一个事件。我发现像C# - TabPage Color eventC# Winform: How to set the Base Color of a TabControl (not the tabpage)的答案,但使用这些集所有的颜色,而不是一个。
所以我希望有一种方法可以实现这一点,我希望改变标签作为一个方法,而不是一个事件?
类似于:

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

enxuqcxy1#

如果你想给选项卡上色,请尝试以下代码:

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

acruukt92#

对于正在阅读这篇文章的WinForms用户-这只在你将标签控件的DrawMode设置为OwnerDrawFixed时有效-如果DrawItem事件设置为Normal,它就不会触发。

628mspwn

628mspwn3#

要添加到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
特别感谢这个精彩的blog entry,它描述了如何在表单上旋转文本。

jqjz2hbq

jqjz2hbq4#

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

     }
}
jw5wzhpr

jw5wzhpr5#

如果任何一个需要把颜色的标签头使用试试这个。我的标签名称tabControl

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

在主类下declear this,

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

声明此函数,它将生成输出
final out

wvmv3b1j

wvmv3b1j6#

Samy的回答的详细说明......如果您需要一个单独的标签突出显示不同的颜色,例如警报标签的粉红色,那么其他标签功能正常,但仍然突出显示为焦点,我添加如下:

private void tabControlMain_DrawItem(object sender, DrawItemEventArgs e)
        {

            if (e.Index == iTabIndexDiffColor)
            {
                Color cColor =  Color.LightPink;
                using (Brush br = new SolidBrush(cColor))
                {
                    e.Graphics.FillRectangle(br, e.Bounds);
                    SizeF sz = e.Graphics.MeasureString(tabControlMain.TabPages[e.Index].Text, e.Font);
                    e.Graphics.DrawString(tabControlMain.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();
                }
            }
            else
            {
                e.DrawBackground();
                Color color; Color tabTextColor;
                if (e.Index == tabControlMain.SelectedIndex)
                    color = Color.White;
                else
                {
                    tabTextColor = Color.FromArgb(0x000001);
                    color = Color.FromArgb(tabTextColor.R, tabTextColor.G, tabTextColor.B);
                }
                TextRenderer.DrawText(e.Graphics, tabControlMain.TabPages[e.Index].Text, e.Font, e.Bounds, color);
            }
        }`

相关问题