I have created a custom tab control.The codes are : THE CODE IS KINNDA HUGE,SO PLEASE BE PATIENT TO READ THE ENTIRE CODE
Public Class mytab
Inherits System.Windows.Forms.TabControl
Dim MainColor As Color
Dim TextColor As Color
Dim LightTheme As Boolean
Public Property UseLightTheme() As Boolean
Get
Return LightTheme
End Get
Set(state As Boolean)
LightTheme = state
Refresh()
End Set
End Property
Public Property TabColor As Color
Get
Return MainColor
End Get
Set(col As Color)
MainColor = col
Refresh()
End Set
End Property
Public Property FontColor As Color
Get
Return TextColor
End Get
Set(col As Color)
TextColor = col
Refresh()
End Set
End Property
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
DoubleBuffered = True
Anchor = AnchorStyles.Top And AnchorStyles.Right And AnchorStyles.Bottom And AnchorStyles.Left
SizeMode = TabSizeMode.Fixed
ItemSize = New Size(100, 29)
Size = New Size(400, 250)
MainColor = Color.FromArgb(59, 151, 241)
TextColor = Color.White
LightTheme = False
Alignment = TabAlignment.Top
End Sub
Function ToPen(ByVal color As Color) As Pen
Return New Pen(color)
End Function
Function ToBrush(ByVal color As Color) As Brush
Return New SolidBrush(color)
End Function
Protected Overrides Sub onpaint(ByVal e As PaintEventArgs)
Dim B As New Bitmap(Width, Height)
Dim G As Graphics = Graphics.FromImage(B)
'tabpage color
If LightTheme = False Then
Try : SelectedTab.BackColor = Color.Azure : Catch : End Try
G.Clear(Color.Azure)
Else
Try : SelectedTab.BackColor = Color.FromArgb(80, 80, 97) : Catch : End Try
G.Clear(Color.FromArgb(80, 80, 97))
End If
'tab bar
If LightTheme = False Then
If Alignment = TabAlignment.Top Then
G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), New Rectangle(0, 0, Width, ItemSize.Height + 5))
ElseIf Alignment = TabAlignment.Bottom Then
G.FillRectangle(New SolidBrush(Color.FromArgb(255, 255, 255, 255)), New Rectangle(0, Height - ItemSize.Height, Width, ItemSize.Height))
ElseIf Alignment = TabAlignment.Left Then
G.FillRectangle(New SolidBrush(Color.FromArgb(255, 255, 255, 255)), New Rectangle(0, 0, ItemSize.Height, Height))
ElseIf Alignment = TabAlignment.Right Then
G.FillRectangle(New SolidBrush(Color.FromArgb(255, 255, 255, 255)), New Rectangle(Width - ItemSize.Height, 0, ItemSize.Height, Height))
End If
Else
If Alignment = TabAlignment.Top Then
G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), New Rectangle(0, 0, Width, ItemSize.Height + 5))
ElseIf Alignment = TabAlignment.Bottom Then
G.FillRectangle(New SolidBrush(Color.FromArgb(255, 200, 200, 200)), New Rectangle(0, Height - ItemSize.Height, Width, ItemSize.Height))
ElseIf Alignment = TabAlignment.Left Then
G.FillRectangle(New SolidBrush(Color.FromArgb(255, 200, 200, 200)), New Rectangle(0, 0, ItemSize.Height, Height))
ElseIf Alignment = TabAlignment.Right Then
G.FillRectangle(New SolidBrush(Color.FromArgb(255, 200, 200, 200)), New Rectangle(Width - ItemSize.Height, 0, ItemSize.Height, Height))
End If
End If
'selected tab stuff
Dim b2 As New SolidBrush(Color.FromArgb(44, 170, 250))
For i = 0 To TabCount - 1
If i = SelectedIndex Then
Dim x2 As Rectangle = New Rectangle(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2, GetTabRect(i).Width, GetTabRect(i).Height + 4)
Dim myblend1 As New ColorBlend()
myblend1.Colors = {Color.FromArgb(44, 170, 250), Color.FromArgb(44, 170, 250), Color.FromArgb(44, 170, 250)} 'colors
myblend1.Positions = {0.0F, 0.5F, 1.0F}
Dim lgBrush1 As New LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F)
lgBrush1.InterpolationColors = myblend1
G.FillRectangle(lgBrush1, x2)
Dim tabTextArea As Rectangle = GetTabRect(i)
e.Graphics.FillRectangle(b2, tabTextArea)
Using br As New SolidBrush(Color.FromArgb(80, 80, 97))
TextRenderer.DrawText(e.Graphics, TabPages(i).Text, New Font("Segoe UI light", 10.0), tabTextArea, TabPages(i).ForeColor)
End Using
If ImageList IsNot Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X, x2.Location.Y))
G.DrawString(" " & TabPages(i).Text, Font, Me.ToBrush(Me.TextColor), x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular), Me.ToBrush(Me.TextColor), x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular), Me.ToBrush(Me.TextColor), x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular), Me.ToBrush(Me.TextColor), x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
If LightTheme = False Then
G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 2, x2.Location.Y - 1), New Point(x2.Location.X, x2.Location.Y))
G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
Else
G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 2, x2.Location.Y - 11), New Point(x2.Location.X, x2.Location.Y))
G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
End If
Else
Dim x1 As Rectangle = New Rectangle(GetTabRect(i).Location.X, GetTabRect(i).Location.Y, GetTabRect(i).Width, GetTabRect(i).Height )
' unselected tab color
If LightTheme = False Then
G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), x1)
Else
G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), x1)
End If
If LightTheme = False Then
If ImageList IsNot Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x1.Location.X, x1.Location.Y))
G.DrawString(" " & TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
G.DrawString(TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
G.DrawString(TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
G.DrawString(TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
ElseIf LightTheme = True Then
If ImageList IsNot Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x1.Location.X, x1.Location.Y))
G.DrawString(" " & TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
G.DrawString(TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
G.DrawString(TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
G.DrawString(TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
'................................................................................................
End If
End If
Next
e.Graphics.DrawImage(B.Clone, 0, 0)
G.Dispose() : B.Dispose()
End Sub
End Class
What i want is, when i hover over an unselected tab,the tab header color would change like the built-in tabcontrol.Is it possible ?
A few more things i'ld like to know.I hope all of guys have used recent microsoft products like office 16,outlook 16 . The tabcontrol that these softwares contain some animations. I mean when u click on a tab(from the menubar), the color smoothly fades in unlike the tabcontrol i made where the color changes immidietly when i select the tab..Recently i have been working on WPF(XAML DESIGNING i mean) so i am preety much sure that i can achieve what i want(i mean the kinnda animation that Office/OutLook has) in WPF. But is Office/OutLook WPF applications ? Is there no way to achieve these animated looks in WinForms because creating the same TabControl i made in wpf would require a lot of XAML coding, maybe beyond my knowledge ..
Any Help ?