用于自定义 CheckBox 控件外观的公共属性。
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
#Region "References"
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
#End Region
Namespace FlatUI
''' <summary>
''' Flat CheckBox control
''' </summary>
Public NotInheritable Class FlatCheckBox
Inherits CheckBox
#Region "Private Members"
Dim _theme As UITheme = UITheme.LightBlue
Private _boxNormalBackColor As Color = Color.FromArgb(243, 249, 255)
Private _boxFocusBackColor As Color = Color.FromArgb(243, 249, 255)
Private _boxMouseDownBackColor As Color = Color.FromArgb(51, 153, 255)
Private _boxNormalBorderColor As Color = Color.FromArgb(30, 30, 30)
Private _boxFocusBorderColor As Color = Color.FromArgb(51, 153, 255)
Private _boxMouseDownBorderColor As Color = Color.FromArgb(51, 153, 255)
Private _checkMarkNormalColor As Color = Color.FromArgb(30, 30, 30)
Private _checkMarkFocusColor As Color = Color.FromArgb(30, 30, 30)
Private _checkMarkMouseDownColor As Color = Color.FromArgb(243, 249, 255)
Private _focusCuesColor As Color = Color.Gray
Private _isMouseEnter As Boolean
Private _isMouseDown As Boolean
#End Region
#Region "Constants"
Const TEXT_X = 21
Const TEXT_Y = 2
Const BOX_X = 4
Const BOX_Y = 4
Const BOX_WIDTH = 12
Const BOX_HEIGHT = 12
#End Region
#Region "Public Methods"
Public Sub New()
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
BackColor = Color.FromArgb(251, 251, 251)
ForeColor = Color.FromArgb(30, 30, 30)
Appearance = Appearance.Button
FlatStyle = FlatStyle.Flat
FlatAppearance.BorderSize = 0
DoubleBuffered = True
UseVisualStyleBackColor = False
End Sub
#End Region
#Region "Public Properties"
<Localizable(True)> <Category("Flat CheckBox")> <Description("The box backcolor in the normal state.")> <DefaultValue(GetType(Color), "243, 249, 255")>
Public Property BoxNormalBackColor As Color
Get
Return _boxNormalBackColor
End Get
Set(value As Color)
_boxNormalBackColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The box backcolor when the CheckBox control is focused.")> <DefaultValue(GetType(Color), "243, 249, 255")>
Public Property BoxFocusBackColor As Color
Get
Return _boxFocusBackColor
End Get
Set(value As Color)
_boxFocusBackColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The box backcolor when the CheckBox control is clicked.")> <DefaultValue(GetType(Color), "51, 153, 255")>
Public Property BoxMouseDownBackColor As Color
Get
Return _boxMouseDownBackColor
End Get
Set(value As Color)
_boxMouseDownBackColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The box border color in the normal state.")> <DefaultValue(GetType(Color), "30, 30, 30")>
Public Property BoxNormalBorderColor As Color
Get
Return _boxNormalBorderColor
End Get
Set(value As Color)
_boxNormalBorderColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The box border color when the CheckBox control is focused.")> <DefaultValue(GetType(Color), "51, 153, 255")>
Public Property BoxFocusBorderColor As Color
Get
Return _boxFocusBorderColor
End Get
Set(value As Color)
_boxFocusBorderColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The box border color when the CheckBox control is clicked.")> <DefaultValue(GetType(Color), "51, 153, 255")>
Public Property BoxMouseDownBorderColor As Color
Get
Return _boxMouseDownBorderColor
End Get
Set(value As Color)
_boxMouseDownBorderColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox check-mark color in the normal state.")> <DefaultValue(GetType(Color), "30, 30, 30")>
Public Property CheckMarkNormalColor As Color
Get
Return _checkMarkNormalColor
End Get
Set(value As Color)
_checkMarkNormalColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox check-mark color when the CheckBox control is focused.")> <DefaultValue(GetType(Color), "30, 30, 30")>
Public Property CheckMarkFocusColor As Color
Get
Return _checkMarkFocusColor
End Get
Set(value As Color)
_checkMarkFocusColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox check-mark color when the CheckBox control is clicked.")> <DefaultValue(GetType(Color), "243, 249, 255")>
Public Property CheckMarkMouseDownColor As Color
Get
Return _checkMarkMouseDownColor
End Get
Set(value As Color)
_checkMarkMouseDownColor = value
Invalidate()
End Set
End Property
<Localizable(True)> <Category("Flat CheckBox")> <Description("The CheckBox control focus cues color.")> <DefaultValue(GetType(Color), "Gray")>
Public Property FocusCuesColor As Color
Get
Return _focusCuesColor
End Get
Set(value As Color)
_focusCuesColor = value
Invalidate()
End Set
End Property
<Category("Flat CheckBox")> <Description("The general style of the Flat CheckBox control.")> <DefaultValue(GetType(UITheme), "1")> Public Property Theme As UITheme
Get
Return _theme
End Get
Set(value As UITheme)
_theme = value
If _theme = UITheme.DarkBlue Then
BackColor = Color.FromArgb(37, 37, 38)
ForeColor = Color.Gainsboro
BoxNormalBackColor = Color.FromArgb(31, 31, 32)
BoxFocusBackColor = Color.FromArgb(31, 31, 32)
BoxMouseDownBackColor = Color.FromArgb(0, 122, 204)
BoxNormalBorderColor = Color.Gainsboro
BoxFocusBorderColor = Color.FromArgb(0, 122, 204)
BoxMouseDownBorderColor = Color.FromArgb(0, 122, 204)
CheckMarkNormalColor = Color.FromArgb(241, 241, 241)
CheckMarkFocusColor = Color.FromArgb(241, 241, 241)
CheckMarkMouseDownColor = Color.FromArgb(241, 241, 241)
FocusCuesColor = Color.Gray
ElseIf _theme = UITheme.LightBlue Then
BackColor = Color.FromArgb(251, 251, 251)
ForeColor = Color.FromArgb(30, 30, 30)
BoxNormalBackColor = Color.FromArgb(243, 249, 255)
BoxFocusBackColor = Color.FromArgb(243, 249, 255)
BoxMouseDownBackColor = Color.FromArgb(51, 153, 255)
BoxNormalBorderColor = Color.FromArgb(30, 30, 30)
BoxFocusBorderColor = Color.FromArgb(51, 153, 255)
BoxMouseDownBorderColor = Color.FromArgb(51, 153, 255)
CheckMarkNormalColor = Color.FromArgb(30, 30, 30)
CheckMarkFocusColor = Color.FromArgb(30, 30, 30)
CheckMarkMouseDownColor = Color.FromArgb(243, 249, 255)
FocusCuesColor = Color.Gray
End If
Invalidate()
End Set
End Property
#End Region
#Region "Private Methods"
Private Sub ResizeForAutoSize()
If AutoSize Then SetBoundsCore(Left, Top, Width, Height, BoundsSpecified.Size)
End Sub
Private Function GetAutoSize() As Size
Dim g = CreateGraphics()
Dim textSize = g.MeasureString(Text, Font)
Dim cntrlSize As New Size(CInt(textSize.Width + TEXT_X), CInt(textSize.Height + (TEXT_Y * 2)))
Return cntrlSize
End Function
#End Region
#Region "Overridden Methods"
Protected Overrides Sub OnPaint(e As PaintEventArgs)
#Disable Warning CA1062 ' Validate arguments of public methods
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
#Enable Warning CA1062 ' Validate arguments of public methods
e.Graphics.Clear(BackColor)
'Draw CheckBox text
Using b As New SolidBrush(ForeColor)
e.Graphics.DrawString(Text, Font, b, TEXT_X, TEXT_Y)
End Using
'Draw box background
Dim boxBackgroundColor As Color
If _isMouseDown Then
boxBackgroundColor = _boxMouseDownBackColor
ElseIf Focused OrElse _isMouseEnter Then
boxBackgroundColor = _boxFocusBackColor
Else
boxBackgroundColor = _boxNormalBackColor
End If
Using b As New SolidBrush(boxBackgroundColor)
e.Graphics.FillRectangle(b, BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT)
End Using
'Draw check mark
If Checked Then
Dim checkMarkColor As Color
If _isMouseDown Then
checkMarkColor = _checkMarkMouseDownColor
ElseIf Focused OrElse _isMouseEnter Then
checkMarkColor = _checkMarkFocusColor
Else
checkMarkColor = _checkMarkNormalColor
End If
Dim rectCheck = New Rectangle(BOX_X + 2, BOX_Y + 3, BOX_WIDTH - 4, BOX_HEIGHT - 6)
Using p = New Pen(checkMarkColor, 2)
e.Graphics.DrawLines(p, New Point() {New Point(rectCheck.Left, rectCheck.Bottom - CInt(rectCheck.Height / 2)), New Point(rectCheck.Left + CInt(rectCheck.Width / 3), rectCheck.Bottom), New Point(rectCheck.Right, rectCheck.Top)})
End Using
End If
'Draw box border
Dim boxBorderColor As Color
If _isMouseDown Then
boxBorderColor = _boxMouseDownBorderColor
ElseIf Focused OrElse _isMouseEnter Then
boxBorderColor = _boxFocusBorderColor
Else
boxBorderColor = _boxNormalBorderColor
End If
Using p = New Pen(boxBorderColor)
e.Graphics.DrawRectangle(p, BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT)
End Using
'Draw focus cues
If Focused Then
Dim textSize = e.Graphics.MeasureString(Text, Font)
Dim focusRect = New Rectangle(TEXT_X - 1, TEXT_Y - 1, CInt(textSize.Width), CInt(textSize.Height))
focusRect.Inflate(-1, -1)
Using p As New Pen(_focusCuesColor) With {.DashStyle = DashStyle.Dot}
e.Graphics.SmoothingMode = SmoothingMode.Default
e.Graphics.DrawRectangle(p, focusRect)
End Using
End If
ResizeForAutoSize()
End Sub
Public Overrides Function GetPreferredSize(proposedSize As Size) As Size
Return GetAutoSize()
End Function
Protected Overrides Sub SetBoundsCore(x As Integer, y As Integer, width As Integer, height As Integer, specified As BoundsSpecified)
If AutoSize AndAlso (specified And BoundsSpecified.Size) <> 0 Then
Dim cntrlSize = GetAutoSize()
width = cntrlSize.Width
height = cntrlSize.Height
End If
MyBase.SetBoundsCore(x, y, width, height, specified)
End Sub
Protected Overrides Sub OnResize(e As EventArgs)
MyBase.OnResize(e)
Invalidate()
End Sub
Protected Overrides Sub OnMouseEnter(eventargs As EventArgs)
MyBase.OnMouseEnter(eventargs)
_isMouseEnter = True
Invalidate()
End Sub
Protected Overrides Sub OnMouseLeave(eventargs As EventArgs)
MyBase.OnMouseLeave(eventargs)
_isMouseEnter = False
Invalidate()
End Sub
Protected Overrides Sub OnMouseDown(mevent As MouseEventArgs)
MyBase.OnMouseDown(mevent)
_isMouseDown = True
Invalidate()
End Sub
Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
MyBase.OnMouseUp(mevent)
_isMouseDown = False
Invalidate()
End Sub
<DefaultValue(GetType(Color), "251, 251, 251")> Public Overrides Property BackColor As Color
Get
Return MyBase.BackColor
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
<DefaultValue(GetType(Color), "30, 30, 30")> Public Overrides Property ForeColor As Color
Get
Return MyBase.ForeColor
End Get
Set(value As Color)
MyBase.ForeColor = value
End Set
End Property
#End Region
End Class
End Namespace