[VB.Net] TabControlDesigner 1.0.0
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Diagnostics
Imports System.Drawing.Drawing2D
'-------------------------------------
'TabControlDesigner
'
'Creator: Eprouvez
'Date: 6/5/2012
'Version: 1.0.0
'
'Credits to:
'Aeonhack
'mavamaarten
'
'Coming soon:
'Tab customization and sharing
'More helpful methods
'Make Tab offsets easier to work with
'--------------------------------------
MustInherit Class TabControlDesigner
Inherits TabControl
Protected G As Graphics
Protected State As MouseState
Protected Coordinates As Point
Public Enum MouseState As Integer
None = 0
Over = 1
Down = 2
End Enum
#Region " Routines "
Public Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer Or ControlStyles.SupportsTransparentBackColor, True)
DoubleBuffered = True
SizeMode = TabSizeMode.Fixed
ItemSize = New Size(250, 26)
End Sub
Public Sub SetState(current As MouseState)
State = current
Invalidate()
End Sub
#End Region
#Region " Hooks "
Protected MustOverride Sub TabPaint(Index As Integer)
Protected Overridable Sub PaintHook()
End Sub
#End Region
#Region " Properties "
Private _InactiveIconOpacity As Integer = 50
Public Property InactiveIconOpacity() As Integer
Get
Return _InactiveIconOpacity
End Get
Set
If (value Is Nothing) Then
value = 50
End If
If value < 0 Then
value = 0
End If
If value > 100 Then
value = 100
End If
_InactiveIconOpacity = value
End Set
End Property
Private _PanelColor As Color
Public Property PanelColor() As Color
Get
Return _PanelColor
End Get
Set
_PanelColor = value
End Set
End Property
#End Region
#Region " Drawing Methods "
Public Function Measure(Text As String) As SizeF
Return G.MeasureString(Text, Font)
End Function
Public Function Measure(Text As String, Font As Font) As SizeF
Return G.MeasureString(Text, Font)
End Function
Public Function Center(Text As String, Area As Rectangle) As Point
Return Center(Text, Font, Area)
End Function
Public Function Center(Text As String, Font As Font, Area As Rectangle) As Point
Return Center(Text, Font, Area, 0, 0)
End Function
Public Function Center(Text As String, Font As Font, Area As Rectangle, XOffset As Integer, YOffset As Integer) As Point
Dim M As dynamic = Measure(Text, Font)
Return New Point(Convert.ToInt32(Area.X + Area.Width / 2 - M.Width / 2) + XOffset, Convert.ToInt32(Area.Y + Area.Height / 2 - M.Height / 2) + YOffset)
End Function
Public Function ToPen(color As Color) As Pen
Return New Pen(color)
End Function
Public Function ToPen(color As Color, width As Single) As Pen
Return New Pen(color, width)
End Function
Public Function ToBrush(color As Color) As Brush
Return New SolidBrush(color)
End Function
Public Function RGB(Red As Integer, Green As Integer, Blue As Integer) As Color
Return Color.FromArgb(Red, Green, Blue)
End Function
Public Function ARGB(Alpha As Integer, color As Color) As Color
Return color.FromArgb(Alpha, color)
End Function
Public Function ARGB(Alpha As Integer, Red As Integer, Green As Integer, Blue As Integer) As Color
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function
Public Function Shrink(rectangle As Rectangle, Offset As Integer) As Rectangle
Return Shrink(rectangle, Offset, True)
End Function
Public Function Shrink(rectangle As Rectangle, Offset As Integer, CenterPoint As Boolean) As Rectangle
Dim O As dynamic = If(CenterPoint = True, Offset, 0)
Dim R As dynamic = New Rectangle(rectangle.X + O, rectangle.Y + O, rectangle.Width - Offset * 2, rectangle.Height - Offset * 2)
Return R
End Function
Public Function Enlarge(rectangle As Rectangle, Offset As Integer) As Rectangle
Return Enlarge(rectangle, Offset, True)
End Function
Public Function Enlarge(rectangle As Rectangle, Offset As Integer, CenterPoint As Boolean) As Rectangle
Dim O As dynamic = If(CenterPoint = True, Offset, 0)
Dim R As dynamic = New Rectangle(rectangle.X - O, rectangle.Y - O, rectangle.Width + Offset * 2, rectangle.Height + Offset * 2)
Return R
End Function
Public Function ImageOpacity(Image As Bitmap, Opacity As Single) As Image
Dim Result As New Bitmap(Image.Width, Image.Height, Imaging.PixelFormat.Format32bppArgb)
Dim _with1 = Image
Opacity = Math.Min(Opacity, 100)
Using Attributes As New Imaging.ImageAttributes()
Dim Matrix As New Imaging.ColorMatrix()
Matrix.Matrix33 = Opacity / 100F
Attributes.SetColorMatrix(Matrix)
Dim Points As PointF() = {New Point(0, 0), New Point(_with1.Width, 0), New Point(0, _with1.Height)}
Using I As Graphics = Graphics.FromImage(Result)
I.Clear(Color.Transparent)
I.DrawImage(Image, Points, New RectangleF(Point.Empty, _with1.Size), GraphicsUnit.Pixel, Attributes)
End Using
End Using
Return Result
End Function
#End Region
#Region " Override Methods "
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
MyBase.DoubleBuffered = True
SizeMode = TabSizeMode.Fixed
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer Or ControlStyles.SupportsTransparentBackColor, True)
End Sub
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
SetState(MouseState.Over)
MyBase.OnMouseHover(e)
End Sub
Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
SetState(MouseState.None)
For I As Integer = 0 To TabPages.Count - 1
If TabPages(I).DisplayRectangle.Contains(Coordinates) Then
MyBase.Invalidate()
' TODO: might not be correct. Was : Exit For
Exit For
End If
Next
MyBase.OnMouseHover(e)
End Sub
Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
Coordinates = e.Location
For I As Integer = 0 To TabPages.Count - 1
If TabPages(I).DisplayRectangle.Contains(Coordinates) Then
MyBase.Invalidate()
' TODO: might not be correct. Was : Exit For
Exit For
End If
Next
MyBase.OnMouseMove(e)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
G = e.Graphics
If (_PanelColor IsNot Nothing) Then
G.FillRectangle(ToBrush(_PanelColor), e.ClipRectangle)
End If
PaintHook()
For I As Integer = 0 To TabPages.Count - 1
TabPaint(I)
Next
End Sub
#End Region
End Class
Imports System.Collections.Generic
Imports System.Data
Imports System.Diagnostics
Imports System.Drawing.Drawing2D
'-------------------------------------
'TabControlDesigner
'
'Creator: Eprouvez
'Date: 6/5/2012
'Version: 1.0.0
'
'Credits to:
'Aeonhack
'mavamaarten
'
'Coming soon:
'Tab customization and sharing
'More helpful methods
'Make Tab offsets easier to work with
'--------------------------------------
MustInherit Class TabControlDesigner
Inherits TabControl
Protected G As Graphics
Protected State As MouseState
Protected Coordinates As Point
Public Enum MouseState As Integer
None = 0
Over = 1
Down = 2
End Enum
#Region " Routines "
Public Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer Or ControlStyles.SupportsTransparentBackColor, True)
DoubleBuffered = True
SizeMode = TabSizeMode.Fixed
ItemSize = New Size(250, 26)
End Sub
Public Sub SetState(current As MouseState)
State = current
Invalidate()
End Sub
#End Region
#Region " Hooks "
Protected MustOverride Sub TabPaint(Index As Integer)
Protected Overridable Sub PaintHook()
End Sub
#End Region
#Region " Properties "
Private _InactiveIconOpacity As Integer = 50
Public Property InactiveIconOpacity() As Integer
Get
Return _InactiveIconOpacity
End Get
Set
If (value Is Nothing) Then
value = 50
End If
If value < 0 Then
value = 0
End If
If value > 100 Then
value = 100
End If
_InactiveIconOpacity = value
End Set
End Property
Private _PanelColor As Color
Public Property PanelColor() As Color
Get
Return _PanelColor
End Get
Set
_PanelColor = value
End Set
End Property
#End Region
#Region " Drawing Methods "
Public Function Measure(Text As String) As SizeF
Return G.MeasureString(Text, Font)
End Function
Public Function Measure(Text As String, Font As Font) As SizeF
Return G.MeasureString(Text, Font)
End Function
Public Function Center(Text As String, Area As Rectangle) As Point
Return Center(Text, Font, Area)
End Function
Public Function Center(Text As String, Font As Font, Area As Rectangle) As Point
Return Center(Text, Font, Area, 0, 0)
End Function
Public Function Center(Text As String, Font As Font, Area As Rectangle, XOffset As Integer, YOffset As Integer) As Point
Dim M As dynamic = Measure(Text, Font)
Return New Point(Convert.ToInt32(Area.X + Area.Width / 2 - M.Width / 2) + XOffset, Convert.ToInt32(Area.Y + Area.Height / 2 - M.Height / 2) + YOffset)
End Function
Public Function ToPen(color As Color) As Pen
Return New Pen(color)
End Function
Public Function ToPen(color As Color, width As Single) As Pen
Return New Pen(color, width)
End Function
Public Function ToBrush(color As Color) As Brush
Return New SolidBrush(color)
End Function
Public Function RGB(Red As Integer, Green As Integer, Blue As Integer) As Color
Return Color.FromArgb(Red, Green, Blue)
End Function
Public Function ARGB(Alpha As Integer, color As Color) As Color
Return color.FromArgb(Alpha, color)
End Function
Public Function ARGB(Alpha As Integer, Red As Integer, Green As Integer, Blue As Integer) As Color
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function
Public Function Shrink(rectangle As Rectangle, Offset As Integer) As Rectangle
Return Shrink(rectangle, Offset, True)
End Function
Public Function Shrink(rectangle As Rectangle, Offset As Integer, CenterPoint As Boolean) As Rectangle
Dim O As dynamic = If(CenterPoint = True, Offset, 0)
Dim R As dynamic = New Rectangle(rectangle.X + O, rectangle.Y + O, rectangle.Width - Offset * 2, rectangle.Height - Offset * 2)
Return R
End Function
Public Function Enlarge(rectangle As Rectangle, Offset As Integer) As Rectangle
Return Enlarge(rectangle, Offset, True)
End Function
Public Function Enlarge(rectangle As Rectangle, Offset As Integer, CenterPoint As Boolean) As Rectangle
Dim O As dynamic = If(CenterPoint = True, Offset, 0)
Dim R As dynamic = New Rectangle(rectangle.X - O, rectangle.Y - O, rectangle.Width + Offset * 2, rectangle.Height + Offset * 2)
Return R
End Function
Public Function ImageOpacity(Image As Bitmap, Opacity As Single) As Image
Dim Result As New Bitmap(Image.Width, Image.Height, Imaging.PixelFormat.Format32bppArgb)
Dim _with1 = Image
Opacity = Math.Min(Opacity, 100)
Using Attributes As New Imaging.ImageAttributes()
Dim Matrix As New Imaging.ColorMatrix()
Matrix.Matrix33 = Opacity / 100F
Attributes.SetColorMatrix(Matrix)
Dim Points As PointF() = {New Point(0, 0), New Point(_with1.Width, 0), New Point(0, _with1.Height)}
Using I As Graphics = Graphics.FromImage(Result)
I.Clear(Color.Transparent)
I.DrawImage(Image, Points, New RectangleF(Point.Empty, _with1.Size), GraphicsUnit.Pixel, Attributes)
End Using
End Using
Return Result
End Function
#End Region
#Region " Override Methods "
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
MyBase.DoubleBuffered = True
SizeMode = TabSizeMode.Fixed
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer Or ControlStyles.SupportsTransparentBackColor, True)
End Sub
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
SetState(MouseState.Over)
MyBase.OnMouseHover(e)
End Sub
Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
SetState(MouseState.None)
For I As Integer = 0 To TabPages.Count - 1
If TabPages(I).DisplayRectangle.Contains(Coordinates) Then
MyBase.Invalidate()
' TODO: might not be correct. Was : Exit For
Exit For
End If
Next
MyBase.OnMouseHover(e)
End Sub
Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
Coordinates = e.Location
For I As Integer = 0 To TabPages.Count - 1
If TabPages(I).DisplayRectangle.Contains(Coordinates) Then
MyBase.Invalidate()
' TODO: might not be correct. Was : Exit For
Exit For
End If
Next
MyBase.OnMouseMove(e)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
G = e.Graphics
If (_PanelColor IsNot Nothing) Then
G.FillRectangle(ToBrush(_PanelColor), e.ClipRectangle)
End If
PaintHook()
For I As Integer = 0 To TabPages.Count - 1
TabPaint(I)
Next
End Sub
#End Region
End Class