Set Tabpage Header Color

Set TabPage Header Color

If you want to color the tabs, try the following code:

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

visual C# Change tab header colour

The DrawItemEventArgs e parameter will tell you all you need.

To draw ther headers in various colors replace Brushes.Black by myBrush and put the DrawString in a using clause like this:

using (SolidBrush myBrush = new SolidBrush (tabControl1.TabPages[e.Index].ForeColor))
{
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, myBrush ,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
}

Now each header will be drawn in the ForeColor of its TabPage.

Replacing the DrawString by a TextRenderer.DrawText would be even better!

If you only want to change the color of the selected tab simply use a check like this:

SolidBrush myBrush = new SolidBrush (e.State.HasFlag(DrawItemState.Selected) ? 
SystemColors.ActiveCaptionText : SystemColors.ControlText)

C# WindowsForm - How to change a tabpages headers text color based on how dark the background color is?

Set the draw mode of you tab page:

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

And then:

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
var color = GetDesiredColor(e.Index); // TODO: Implement it for yourself
TextRenderer.DrawText(e.Graphics, tabControl1.TabPages[e.Index].Text, e.Font, e.Bounds, color);
}

Of course, you might want to adjust the bounds, too.

How do I change the color of each tab?

There are two things that you need to do:

First is to change the DrawMode of the TabControl and set it to OwnerDrawFixed

drawmode

And the second is to handle the TabControl DrawItem event

Here is an example:

 Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles TabControl1.DrawItem
Select Case e.Index
Case 0
e.Graphics.FillRectangle(New SolidBrush(Color.Red), e.Bounds)
Case 1
e.Graphics.FillRectangle(New SolidBrush(Color.Blue), e.Bounds)
Case 2
e.Graphics.FillRectangle(New SolidBrush(Color.Magenta), e.Bounds)

End Select

Dim paddedBounds As Rectangle = e.Bounds
paddedBounds.Inflate(-2, -2)
e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, SystemBrushes.HighlightText, paddedBounds)

End Sub

And here is what it looks like (I change the tab colors of the first three tab pages only, the others can be done easily by adding new cases to select case)

tab colors

Change the background of TabControl Header in C# Winform

I solved it myself.
I used this control http://dotnetrix.co.uk/controls.htm
It has a TabControl2 that supports the transparent background for TabControl.
Now it looks pretty good.

Sample Image

WPF change tab header color

Your problem lies in the Controls.TabControl.xaml of MahApps.Metro. Most of the design lies in a template. As you can see in line 227 and 274, the Foreground is not bound to any property like done with other properties like Underline or HeaderFontSize.

This means you can't style these properties explicit without creating a whole new template. Since dynamic resources are used as color a solution is to override the used resources. Here is a workaround to change the colors for a tab item like required:

<TabItem Header="TabItem1">
<TabItem.Resources>
<SolidColorBrush x:Key="AccentColorBrush" Color="SteelBlue"/>
<SolidColorBrush x:Key="HighlightBrush" Color="SteelBlue"/>
</TabItem.Resources>
</TabItem>

How to change background color of TabControl.Header

Adding the following style in the TabControl.Resources section should do what you want:

<Style TargetType="{x:Type TabPanel}">
<Setter Property="Background" Value="Black"/>
</Style>

TabControl Custom Tab Bar Background Color

I added the following code to my SettingsTabControl_DrawItem method, which fixes this specific issue. I still have a border color issue, but I guess I can live with that.

Sample Image

        //draw rectangle behind the tabs
Rectangle lastTabRect = SettingsTabControl.GetTabRect( SettingsTabControl.TabPages.Count - 1 );
Rectangle background = new Rectangle();
background.Location = new Point( lastTabRect.Right, 0 );

//pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs
background.Size = new Size( SettingsTabControl.Right - background.Left, lastTabRect.Height + 1 );

using (SolidBrush b = new SolidBrush( Colors.Get( Item.BorderBackground ) ))
{
e.Graphics.FillRectangle( b, background );
}

From here.



Related Topics



Leave a reply



Submit