ControlC ControlC.com

[VB.Net] TabControlDesigner 1.1.0

Views: 30Total Chars: 10,995
Created: 6/6/12 1:48:34AM ET
Self Destructs: Never
Imports System.Drawing.Drawing2D, System.IO

'-------------------------------------
'TabControlDesigner
'
'Creator: Eprouvez
'Date: 6/5/2012
'Updated: 6/6/2012
'Version: 1.1.0
'
'Credits to:
'Aeonhack
'mavamaarten
'
'Coming soon:
'More helpful methods
'Make Tab offsets easier to work with
'--------------------------------------

Class Bloom

Sub New(ByVal Name As String, ByVal Color As Color)
_Name = Name
_Value = Color
End Sub
Sub New(ByVal Name As String, ByVal Red As Integer, ByVal Green As Integer, ByVal Blue As Integer)
_Name = Name
_Value = Color.FromArgb(Red, Green, Blue)
End Sub
Sub New(ByVal Name As String, ByVal Alpha As Integer, ByVal Red As Integer, ByVal Green As Integer, ByVal Blue As Integer)
_Name = Name
_Value = Color.FromArgb(Alpha, Red, Green, Blue)
End Sub

Private _Name As String
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property

Private _Value As Color
Public Property Color() As Color
Get
Return _Value
End Get
Set(ByVal value As Color)
_Value = value
End Set
End Property

End Class

MustInherit Class TabControlDesigner
Inherits TabControl

Protected G As Graphics
Protected State As MouseState
Protected Coordinates As Point

Enum MouseState As Integer
None = 0
Over = 1
Down = 2
End Enum

#Region " Routines "

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

Sub SetState(ByVal current As MouseState)
State = current
Invalidate()
End Sub

#End Region

#Region " Hooks "

Protected MustOverride Sub TabPaint(ByVal Index As Integer)

Protected MustOverride Sub ColorHook()
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(ByVal value As Integer)
If IsNothing(value) Then value = 50
If value < 0 Then value = 0
If value > 100 Then value = 100
_InactiveIconOpacity = value
End Set
End Property

Private _PanelColor As Color
Public Property PanelColor() As Color
Get
Return _PanelColor
End Get
Set(ByVal value As Color)
_PanelColor = value
End Set
End Property

Private Items As New Dictionary(Of String, Color)
Property Colors() As Bloom()
Get
Dim T As New List(Of Bloom)
Dim E As Dictionary(Of String, Color).Enumerator = Items.GetEnumerator

While E.MoveNext
T.Add(New Bloom(E.Current.Key, E.Current.Value))
End While

Return T.ToArray
End Get
Set(ByVal value As Bloom())
For Each B As Bloom In value
If Items.ContainsKey(B.Name) Then Items(B.Name) = B.Color
Next

InvalidateCustimization()
ColorHook()
Invalidate()
End Set
End Property

Private _Customization As String
Property Customization() As String
Get
Return _Customization
End Get
Set(ByVal value As String)
If value = _Customization Then Return

Dim Data As Byte()
Dim Items As Bloom() = Colors

Try
Data = Convert.FromBase64String(value)
For I As Integer = 0 To Items.Length - 1
Items(I).Color = Color.FromArgb(BitConverter.ToInt32(Data, I * 4))
Next
Catch
Return
End Try

_Customization = value

Colors = Items
ColorHook()
Invalidate()
End Set
End Property
#End Region

#Region " Property Helpers "

'Credits to Aeonhack
Protected Function GetPen(ByVal name As String) As Pen
Return New Pen(Items(name))
End Function
Protected Function GetPen(ByVal name As String, ByVal width As Single) As Pen
Return New Pen(Items(name), width)
End Function
Protected Function GetBrush(ByVal name As String) As SolidBrush
Return New SolidBrush(Items(name))
End Function
Protected Function GetColor(ByVal name As String) As Color
Return Items(name)
End Function
Protected Sub SetColor(ByVal name As String, ByVal value As Color)
If Items.ContainsKey(name) Then Items(name) = value Else Items.Add(name, value)
End Sub
Protected Sub SetColor(ByVal name As String, ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
SetColor(name, Color.FromArgb(r, g, b))
End Sub
Protected Sub SetColor(ByVal name As String, ByVal a As Byte, ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
SetColor(name, Color.FromArgb(a, r, g, b))
End Sub
Protected Sub SetColor(ByVal name As String, ByVal a As Byte, ByVal value As Color)
SetColor(name, Color.FromArgb(a, value))
End Sub
Private Sub InvalidateCustimization()
Dim M As New MemoryStream(Items.Count * 4)

For Each B As Bloom In Colors
M.Write(BitConverter.GetBytes(B.Color.ToArgb), 0, 4)
Next

M.Close()
_Customization = Convert.ToBase64String(M.ToArray)
End Sub

#End Region

#Region " Drawing Methods "

Function Measure(ByVal Text As String) As SizeF
Return G.MeasureString(Text, Font)
End Function
Function Measure(ByVal Text As String, ByVal Font As Font) As SizeF
Return G.MeasureString(Text, Font)
End Function
Function Center(ByVal Text As String, ByVal Area As Rectangle) As Point
Return Center(Text, Font, Area)
End Function
Function Center(ByVal Text As String, ByVal Font As Font, ByVal Area As Rectangle) As Point
Return Center(Text, Font, Area, 0, 0)
End Function
Function Center(ByVal Text As String, ByVal Font As Font, ByVal Area As Rectangle, ByVal XOffset As Integer, ByVal YOffset As Integer) As Point
Dim M = Measure(Text, Font)
Return New Point(CInt(Area.X + Area.Width / 2 - M.Width / 2) + XOffset, CInt(Area.Y + Area.Height / 2 - M.Height / 2) + YOffset)
End Function
Function ToPen(ByVal color As Color) As Pen
Return New Pen(color)
End Function
Function ToPen(ByVal color As Color, ByVal width As Single) As Pen
Return New Pen(color, width)
End Function
Function ToBrush(ByVal color As Color) As Brush
Return New SolidBrush(color)
End Function
Function RGB(ByVal Red As Integer, ByVal Green As Integer, ByVal Blue As Integer) As Color
Return Color.FromArgb(Red, Green, Blue)
End Function
Function ARGB(ByVal Alpha As Integer, ByVal color As Color) As Color
Return color.FromArgb(Alpha, color)
End Function
Function ARGB(ByVal Alpha As Integer, ByVal Red As Integer, ByVal Green As Integer, ByVal Blue As Integer) As Color
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function
Function Shrink(ByVal rectangle As Rectangle, ByVal Offset As Integer) As Rectangle
Return Shrink(rectangle, Offset, True)
End Function
Function Shrink(ByVal rectangle As Rectangle, ByVal Offset As Integer, ByVal CenterPoint As Boolean) As Rectangle
Dim O = If(CenterPoint = True, Offset, 0)
Dim R = New Rectangle(rectangle.X + O, rectangle.Y + O, rectangle.Width - Offset * 2, rectangle.Height - Offset * 2)
Return R
End Function
Function Enlarge(ByVal rectangle As Rectangle, ByVal Offset As Integer) As Rectangle
Return Enlarge(rectangle, Offset, True)
End Function
Function Enlarge(ByVal rectangle As Rectangle, ByVal Offset As Integer, ByVal CenterPoint As Boolean) As Rectangle
Dim O = If(CenterPoint = True, Offset, 0)
Dim R = New Rectangle(rectangle.X - O, rectangle.Y - O, rectangle.Width + Offset * 2, rectangle.Height + Offset * 2)
Return R
End Function
Function ImageOpacity(ByVal Image As Bitmap, ByVal Opacity As Single) As Image
Dim Result As New Bitmap(Image.Width, Image.Height, Imaging.PixelFormat.Format32bppArgb)
With Image
Opacity = Math.Min(Opacity, 100)
Using Attributes As New Imaging.ImageAttributes
Dim Matrix As New Imaging.ColorMatrix
Matrix.Matrix33 = Opacity / 100.0F
Attributes.SetColorMatrix(Matrix)
Dim Points() As PointF = {New Point(0, 0), New Point(.Width, 0), New Point(0, .Height)}
Using I As Graphics = Graphics.FromImage(Result)
I.Clear(Color.Transparent)
I.DrawImage(Image, Points, New RectangleF(Point.Empty, .Size), GraphicsUnit.Pixel, Attributes)
End Using
End Using
End With
Return Result
End Function

#End Region

#Region " Override Methods "

Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
MyBase.DoubleBuffered = True
InvalidateCustimization()
ColorHook()
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(ByVal e As System.EventArgs)
SetState(MouseState.Over)
MyBase.OnMouseHover(e)
End Sub
Protected Overrides Sub OnMouseLeave(ByVal 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()
Exit For
End If
Next
MyBase.OnMouseHover(e)
End Sub
Protected Overrides Sub OnMouseMove(ByVal 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()
Exit For
End If
Next
MyBase.OnMouseMove(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
G = e.Graphics

If Not IsNothing(_PanelColor) Then G.FillRectangle(ToBrush(_PanelColor), e.ClipRectangle)

PaintHook()

For I As Integer = 0 To TabPages.Count - 1
TabPaint(I)
Next
End Sub

#End Region

End Class
10,995 chars, 1,227 words, 330 lines·Create new version