Thursday, February 4, 2010

To change the colour of TAB control in .Net

To change the color of Tab control

Add this event handler


1. DrawMode property to OwnerDrawFixed.
2. Override the DrawItem event handler definition.


tabControl2.DrawItem += new DrawItemEventHandler(OnDrawItem);


step 2






private void OnDrawItem(object sender, DrawItemEventArgs e)
{

TabPage CurrentTab = tabControl2.TabPages[e.Index];
Rectangle ItemRect = tabControl2.GetTabRect(e.Index);
SolidBrush FillBrush = new SolidBrush(Color.Red);
SolidBrush TextBrush = new SolidBrush(Color.White);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

//If we are currently painting the Selected TabItem we'll
//change the brush colors and inflate the rectangle.
if (System.Convert.ToBoolean(e.State & DrawItemState.Selected))
{
FillBrush.Color = Color.PaleTurquoise;
TextBrush.Color = Color.Red;
ItemRect.Inflate(2, 2);
}

//Set up rotation for left and right aligned tabs
if (tabControl2.Alignment == TabAlignment.Left || tabControl2.Alignment == TabAlignment.Right)
{
float RotateAngle = 90;
if (tabControl2.Alignment == TabAlignment.Left)
RotateAngle = 270;
PointF cp = new PointF(ItemRect.Left + (ItemRect.Width / 2), ItemRect.Top + (ItemRect.Height / 2));
e.Graphics.TranslateTransform(cp.X, cp.Y);
e.Graphics.RotateTransform(RotateAngle);
ItemRect = new Rectangle(-(ItemRect.Height / 2), -(ItemRect.Width / 2), ItemRect.Height, ItemRect.Width);
}

//Next we'll paint the TabItem with our Fill Brush
e.Graphics.FillRectangle(FillBrush, ItemRect);

//Now draw the text.
e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, (RectangleF)ItemRect, sf);

//Reset any Graphics rotation
e.Graphics.ResetTransform();

//Finally, we should Dispose of our brushes.
FillBrush.Dispose();
TextBrush.Dispose();
}

No comments:

Post a Comment