In order to color the tabs of the tabcontrol in vb.net
just drop a tab control on a form with DrawMode = OwnerDrawFixed and put a couple of tab pages in it
1.
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
2.
3.
'Firstly we'll define some parameters.
4.
Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
5.
Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
6.
Dim FillBrush As New SolidBrush(Color.Red)
7.
Dim TextBrush As New SolidBrush(Color.White)
8.
Dim sf As New StringFormat
9.
sf.Alignment = StringAlignment.Center
10.
sf.LineAlignment = StringAlignment.Center
11.
12.
'If we are currently painting the Selected TabItem we'll
13.
'change the brush colors and inflate the rectangle.
14.
If CBool(e.State And DrawItemState.Selected) Then
15.
FillBrush.Color = Color.White
16.
TextBrush.Color = Color.Red
17.
ItemRect.Inflate(2, 2)
18.
End If
19.
20.
'Set up rotation for left and right aligned tabs
21.
If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
22.
Dim RotateAngle As Single = 90
23.
If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
24.
Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
25.
e.Graphics.TranslateTransform(cp.X, cp.Y)
26.
e.Graphics.RotateTransform(RotateAngle)
27.
ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
28.
End If
29.
30.
'Next we'll paint the TabItem with our Fill Brush
31.
e.Graphics.FillRectangle(FillBrush, ItemRect)
32.
33.
'Now draw the text.
34.
e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)
35.
36.
'Reset any Graphics rotation
37.
e.Graphics.ResetTransform()
38.
39.
'Finally, we should Dispose of our brushes.
40.
FillBrush.Dispose()
41.
TextBrush.Dispose()
42.
End Sub
No comments:
Post a Comment