Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Imports System.Windows.Forms
<DefaultEvent("Click")> _
Public Class ButtonEX
Shadows Event Click(sender As Object, e As EventArgs)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Me.Name = "ButtonEX"
Me.Label1.TextAlign = ContentAlignment.MiddleCenter
_ImageAlign = ContentAlignment.MiddleLeft
_ImageSize = New Size(30, 30)
_TextAlign = ContentAlignment.MiddleCenter
Label1.Text = Me.Text
Me.Font = New Font(Me.Font.Name, 10, FontStyle.Regular)
Label1.Font = Me.Font
Label1.ForeColor = Me.ForeColor
Me.DrawMode = Mode.MouseMode
Me.CornerRadius = 2
End Sub
#Region " Private variables etc "
Private _borderColor As Color = Color.Gray
Private _backRemain1 As Color = Color.Gainsboro
Private _backRemain2 As Color = Color.White
Private _backRemain3 As Color = Color.FromArgb(219, 219, 219)
Private _backRemain4 As Color = Color.FromArgb(243, 243, 243)
Private _Hover As Boolean
Private _CurrentButton As String
Private _Click As Boolean
Private ToolTip As New ToolTip
Private _ToolTipText As String
Private _Text As String
Private _TextAlign As ContentAlignment = ContentAlignment.MiddleCenter
Private _Image As Image
Private _ImageSize As Size
Private _ImageAlign As ContentAlignment
Private _DrawMode As Mode
Private _CornerRadius As Integer
#End Region
#Region " Properties "
<Browsable(False)> _
<EditorBrowsable(EditorBrowsableState.Never)> _
Public Overloads Property BackgroundImage As Image
Get
Return Nothing
End Get
Set(ByVal value As Image)
End Set
End Property
<Browsable(False)> _
<EditorBrowsable(EditorBrowsableState.Never)> _
Public Overloads Property BackgroundImageLayout As ImageLayout
Get
Return Nothing
End Get
Set(ByVal value As ImageLayout)
End Set
End Property
<Category("Appearance")> _
Public Property CornerRadius As Integer
Get
Return _CornerRadius
End Get
Set(ByVal value As Integer)
_CornerRadius = value
End Set
End Property
<Category("Appearance")> _
Public Property DrawMode As Mode
Get
Return _DrawMode
End Get
Set(ByVal value As Mode)
_DrawMode = value
Invalidate()
End Set
End Property
<EditorBrowsable(EditorBrowsableState.Always)> _
<Browsable(True)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
<DefaultValue("ButtonEx")> _
Public Overrides Property Text As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
Label1.Text = _Text
Invalidate()
End Set
End Property
<Category("Appearance")> _
Public Property ToolTipText As String
Get
Return _ToolTipText
End Get
Set(ByVal value As String)
_ToolTipText = value
ToolTip.SetToolTip(Label1, value)
End Set
End Property
<Category("Appearance")> _
Public Property TextAlign As ContentAlignment
Get
Return _TextAlign
End Get
Set(ByVal value As ContentAlignment)
_TextAlign = value
Label1.TextAlign = value
Invalidate()
End Set
End Property
<Category("Appearance")> _
Public Property Image As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
Invalidate()
End Set
End Property
<Category("Appearance")> _
Public Property ImageSize As Size
Get
Return _ImageSize
End Get
Set(ByVal value As Size)
_ImageSize = value
Invalidate()
End Set
End Property
<Category("Appearance")> _
Public Property ImageAlign As ContentAlignment
Get
Return _ImageAlign
End Get
Set(ByVal value As ContentAlignment)
_ImageAlign = value
Invalidate()
End Set
End Property
#End Region
#Region " Paint "
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim rectUpper As Rectangle = New Rectangle(0, 0, Me.Width, Me.Height)
Dim pathUpper As GraphicsPath = GetRoundPath(rectUpper, CornerRadius)
If _Hover = True Then
Using brushUpper As Brush = New LinearGradientBrush(rectUpper, _backRemain2, _backRemain1, LinearGradientMode.Vertical)
e.Graphics.FillPath(brushUpper, pathUpper)
End Using
'Draw border
Dim rectFull As New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
Dim pathFull As GraphicsPath = GetRoundPath(rectFull, CornerRadius)
Using pen As New Pen(_borderColor)
e.Graphics.DrawPath(pen, pathFull)
End Using
ElseIf _Click = True Then
Using brushUpper As Brush = New LinearGradientBrush(rectUpper, Color.Gainsboro, Color.LightGray, LinearGradientMode.Vertical)
e.Graphics.FillPath(brushUpper, pathUpper)
End Using
'Draw border
Dim rectFull As New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
Dim pathFull As GraphicsPath = GetRoundPath(rectFull, CornerRadius)
Using pen As New Pen(_borderColor)
e.Graphics.DrawPath(pen, pathFull)
End Using
Else
If DrawMode = Mode.DefaultMode Then
Using brushUpper As Brush = New LinearGradientBrush(rectUpper, _backRemain4, _backRemain3, LinearGradientMode.Vertical)
e.Graphics.FillPath(brushUpper, pathUpper)
End Using
'Draw border
Dim rectFull As New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
Dim pathFull As GraphicsPath = GetRoundPath(rectFull, CornerRadius)
Using pen As New Pen(_borderColor)
e.Graphics.DrawPath(pen, pathFull)
End Using
End If
End If
DrawImage(e.Graphics)
End Sub
Private Sub SpecialButton_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FontChanged
Label1.Font = Me.Font
End Sub
Private Sub SpecialButton_ForeColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ForeColorChanged
Label1.ForeColor = Me.ForeColor
End Sub
#End Region
#Region " Paint Methodes "
Private Function GetRoundPath(ByVal r As Rectangle, ByVal depth As Integer) As GraphicsPath
Dim graphPath As New GraphicsPath
graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90)
graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90)
graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90)
graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90)
graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth \ 2)
Return graphPath
End Function
Public Sub DrawImage(ByVal gr As Graphics)
If Me.Image Is Nothing Then Return
Dim r As Rectangle = New Rectangle(8, 8, Me.ImageSize.Width, Me.ImageSize.Height)
Select Case Me.ImageAlign
Case ContentAlignment.TopCenter
r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, 8, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.TopRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, 8, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleLeft
r = New Rectangle(4, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleCenter
r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomLeft
r = New Rectangle(8, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomCenter
r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
End Select
gr.DrawImage(Me.Image, r)
End Sub
#End Region
#Region " Mouse Events "
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
_Click = True
_Hover = False
Invalidate()
End Sub
Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
_Hover = True
Invalidate()
End Sub
Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
_Hover = False
Invalidate()
End Sub
Private Sub Label1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
_Click = False
_Hover = False
Invalidate()
End Sub
#End Region
Public Enum Mode
MouseMode = 1
DefaultMode = 2
End Enum
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
RaiseEvent Click(Me, e)
End Sub
End Class