Thursday 13 November 2008

How to change the font forecolor, background font color and background color in a TabControl

Because the tab control does not provide any property to change font background, the font forecolor and the box background I have decided to override the DrawItem event so I can control this tab component when windows is painting it.

This event will help you to set the font background, the font forecolor and the box background of
the tab control.

To use it:
1- Just add this event into your code.
2- on Properties change DrawMode property to OwnerDraw so we can mess around with this event later.
3- Go to the events and in the DrawItem event just type the name of our event;TabAndBackgroundBoxColorHandler.

You can modify more parts of the tabcontrol. you just need to capture the coordinates



private void TabAndBackgroundBoxColorHandler(object sender, DrawItemEventArgs e)
{
//########################################################
//## TabAndBackgroundBoxColorHandler ##
//## ##
//## How to use it: -Just set the tab control from ##
//## DrawMode to OwnerDraw. ##
//## -Add this Event into DrawItem event.##
//## -Set the colors you want. ##
//########################################################

//## Getting The tab control to work with it
System.Windows.Forms.TabControl _tTabControl = ((System.Windows.Forms.TabControl)(sender));//tabControl1;

StringFormat _sfStringFormat = new StringFormat();
string _sTabName = null;
Rectangle _rTabCoordinates = e.Bounds;
//Rectangle _rBoxCoordinates = new Rectangle(e.Bounds.X, e.Bounds.Y, _tTabControl.Width, _tTabControl.Height);
Rectangle _rBoxCoordinates = new Rectangle(0, 0, _tTabControl.Width, _tTabControl.Height);
Font _fTabFont;
Brush _brBackBrush = new SolidBrush(Color.Black); //Set background font color
Brush _brForeBrush = new SolidBrush(Color.AliceBlue);//Set foreground font color
Brush _brBackBox = new SolidBrush(Color.Red);//Set background box color color

//## We check the index, so we can paint the selected tab
if (e.Index == _tTabControl.SelectedIndex)
_fTabFont = new Font(e.Font.FontFamily, e.Font.Size, FontStyle.Bold);
else
_fTabFont = e.Font;

//## Drawing the rectangle for the main box
if (!_tTabControl.TabPages[0].Capture)
{
_tTabControl.TabPages[0].Capture = true;//.BackColor = ((SolidBrush)_brBackBrush).Color;
e.Graphics.FillRectangle(_brBackBox, _rBoxCoordinates);
}
else if (_tTabControl.TabPages.Count==e.Index)
{
_tTabControl.TabPages[0].Capture = false;
}

//e.Graphics.FillRectangle(_brBackBox, _rBoxCoordinates);

//## Getting the tab name so we can work with it.
_sTabName = _tTabControl.TabPages[e.Index].Text;

//## Aligning the text...
_sfStringFormat.Alignment = StringAlignment.Center;

//## This will fill the rectangle where the tab is with the color
//## Set before...
e.Graphics.FillRectangle(_brBackBrush, e.Bounds);

//## Creating the new rectange where T is.
//## +-----+
//## | T |
//## +-----+---------+
//## | P |
//## +---------------+
_rTabCoordinates = new Rectangle(_rTabCoordinates.X, _rTabCoordinates.Y + 3, _rTabCoordinates.Width, _rTabCoordinates.Height - 3);

//## Drawing the rectangle in the TabControl + The aligment...
e.Graphics.DrawString(_sTabName, _fTabFont, _brForeBrush, _rTabCoordinates, _sfStringFormat);

//## Dispose objects.
_sfStringFormat.Dispose();

//## Disposing if we have selected something.
if (e.Index == _tTabControl.SelectedIndex)
{
_fTabFont.Dispose();
_brBackBrush.Dispose();
_brForeBrush.Dispose();
_brBackBox.Dispose();
}
else
{
_brBackBrush.Dispose();
_brForeBrush.Dispose();
_brBackBox.Dispose();
}
}

No comments: