Public Class extButton
Inherits System.Windows.Forms.Button
Private CustomButton As Boolean = False
Private bolMouseOver As Boolean = False
Private bolDrawBox As Boolean = False
Private imgMouseOver As Image = Nothing
Private imgMouseDown As Image = Nothing
Private imgBackGround As Image = Nothing
#Region "**************** Design Properties ****************"
<System.ComponentModel.Category("Custom Design")> _
Public Property setCustomButton() As Boolean
Get
setCustomButton = CustomButton
End Get
Set(ByVal value As Boolean)
If CustomButton <> value Then
CustomButton = value
If CustomButton Then
imgBackGround = Me.BackgroundImage
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
Me.FlatAppearance.BorderSize = 0
Try
Me.FlatAppearance.CheckedBackColor = Me.Parent.BackColor
Me.FlatAppearance.MouseDownBackColor = Me.Parent.BackColor
Me.FlatAppearance.MouseOverBackColor = Me.Parent.BackColor
Catch ex As Exception
Me.FlatAppearance.CheckedBackColor = Color.Transparent
Me.FlatAppearance.MouseDownBackColor = Color.Transparent
Me.FlatAppearance.MouseOverBackColor = Color.Transparent
End Try
Else
Me.FlatStyle = Windows.Forms.FlatStyle.Standard
End If
End If
End Set
End Property
<System.ComponentModel.Category("Custom Design")> _
Public Property DrawBoarder() As Boolean
Get
DrawBoarder = bolDrawBox
End Get
Set(ByVal value As Boolean)
bolDrawBox = value
End Set
End Property
<System.ComponentModel.Category("Custom Design")> _
Public Property ImageNormal() As Image
Get
ImageNormal = imgBackGround
End Get
Set(ByVal value As Image)
imgBackGround = value
Me.BackgroundImage = value
End Set
End Property
<System.ComponentModel.Category("Custom Design")> _
Public Property ImageMouseOver() As Image
Get
ImageMouseOver = imgMouseOver
End Get
Set(ByVal value As Image)
imgMouseOver = value
End Set
End Property
<System.ComponentModel.Category("Custom Design")> _
Public Property ImageMouseDown() As Image
Get
ImageMouseDown = imgMouseDown
End Get
Set(ByVal value As Image)
imgMouseDown = value
End Set
End Property
#End Region
#Region "**************** Mybase Handled events ****************"
Protected Overrides Sub onMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If CustomButton Then
Me.BackgroundImage = imgMouseDown
End If
End Sub
Protected Overrides Sub onMouseEnter(ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
If CustomButton Then
Me.BackgroundImage = imgMouseOver
bolMouseOver = True
End If
End Sub
Protected Overrides Sub onMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
If CustomButton Then
Me.BackgroundImage = imgBackGround
bolMouseOver = False
End If
End Sub
Protected Overrides Sub onMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
If CustomButton Then
If bolMouseOver Then
Me.BackgroundImage = imgMouseOver
Else
Me.BackgroundImage = imgBackGround
End If
End If
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
If CustomButton Then
If e.KeyCode = Keys.Space Then
Me.BackgroundImage = imgMouseDown
End If
End If
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyUp(e)
If CustomButton Then
If e.KeyCode = Keys.Space Then
If bolMouseOver Then
Me.BackgroundImage = imgMouseOver
Else
Me.BackgroundImage = imgBackGround
End If
End If
End If
End Sub
Protected Overrides Sub onPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If bolDrawBox Then
Dim pen As Pen = New Pen(Color.Black, 1)
Dim rect As New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
Dim pp As Drawing2D.GraphicsPath = RoundRectPath(rect, 2)
e.Graphics.DrawPath(pen, pp)
pen.Dispose()
pp.Dispose()
End If
End Sub
#End Region
Public Shared Function RoundRectPath(ByVal rect As Rectangle, ByVal cornerRadius As Integer) As Drawing2D.GraphicsPath
Dim cornerDiameter As Integer = cornerRadius * 2
Dim pp As New Drawing2D.GraphicsPath()
pp.AddLine(rect.Left + cornerRadius, rect.Top, rect.Right - cornerRadius, rect.Top) 'top
pp.AddArc(New Rectangle(rect.Right - cornerDiameter, rect.Top, cornerDiameter, cornerDiameter), 270, 90) 'topRight
pp.AddLine(rect.Right, rect.Top + cornerRadius, rect.Right, rect.Bottom - cornerRadius) 'right
pp.AddArc(New Rectangle(rect.Right - cornerDiameter, rect.Bottom - cornerDiameter, cornerDiameter, cornerDiameter), 0, 90) 'bottomRight
pp.AddLine(rect.Right - cornerRadius, rect.Bottom, rect.Left + cornerRadius, rect.Bottom) 'bottom
pp.AddArc(New Rectangle(rect.Left, rect.Bottom - cornerDiameter, cornerDiameter, cornerDiameter), 90, 90) 'bottomLeft
pp.AddLine(rect.Left, rect.Bottom - cornerRadius, rect.Left, rect.Top + cornerRadius) 'left
pp.AddArc(New Rectangle(rect.Left, rect.Top, cornerDiameter, cornerDiameter), 180, 90) 'topLeft
pp.CloseFigure()
Return pp
End Function
End Class