Imports System.Drawing
Imports System.Drawing.Imaging
Public Class BitmapButton
Inherits System.Windows.Forms.Button
#Region " Windows Form Designer generated code "
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Private _imgNorm As Image
Private _imgOver As Image
Private _imgDown As Image
Private overMouse As Boolean = False
Private downMouse As Boolean = False
#Region " New Sub"
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.ResizeRedraw, True)
MyBase.BackColor = System.Drawing.SystemColors.Control
End Sub
#End Region
#Region " Public Properities"
Public Property ImageNorm() As Image
Get
Return _imgNorm
End Get
Set(ByVal Value As Image)
_imgNorm = Value
Me.Invalidate()
End Set
End Property
Public Property ImageOver() As Image
Get
Return _imgOver
End Get
Set(ByVal Value As Image)
_imgOver = Value
Me.Invalidate()
End Set
End Property
Public Property ImageDown() As Image
Get
Return _imgDown
End Get
Set(ByVal Value As Image)
_imgDown = Value
Me.Invalidate()
End Set
End Property
#End Region
#Region " Protected Overrides Subs"
#Region " OnPaint Sub"
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'check background image
If MyBase.BackgroundImage Is Nothing Then
If Not MyBase.BackColor.ToArgb = Color.Transparent.ToArgb Then
e.Graphics.FillRectangle(New SolidBrush(MyBase.BackColor), 0, 0, MyBase.Width, MyBase.Height)
End If
Else 'paint background image
Dim i As Integer
If MyBase.BackgroundImage.Width < MyBase.Width Then
For i = 0 To MyBase.Width - 1 Step MyBase.BackgroundImage.Width
e.Graphics.DrawImage(MyBase.BackgroundImage, i, 0)
Next
Else : e.Graphics.DrawImage(MyBase.BackgroundImage, 0, 0)
End If
If MyBase.BackgroundImage.Height < MyBase.Height Then
For i = 0 To MyBase.Height - 1 Step MyBase.BackgroundImage.Height
e.Graphics.DrawImage(MyBase.BackgroundImage, 0, i)
Next
Else : e.Graphics.DrawImage(MyBase.BackgroundImage, 0, 0)
End If
End If
If _imgNorm Is Nothing Then
e.Graphics.DrawEllipse(New Pen(MyBase.ForeColor, 1), 0, 0, MyBase.Width - 1, MyBase.Height - 1)
Dim noImage As String = " No" & vbCrLf & "Images"
Dim txtSize As SizeF = e.Graphics.MeasureString(noImage, MyBase.Font)
e.Graphics.DrawString(noImage, MyBase.Font, New SolidBrush(MyBase.ForeColor), CInt((MyBase.Width - txtSize.Width) / 2), CInt((MyBase.Height - txtSize.Height) / 2))
Exit Sub
ElseIf Not _imgNorm Is Nothing Then
If MyBase.Enabled Then
If overMouse = False And downMouse = False Then
e.Graphics.DrawImage(_imgNorm, 0, 0)
ElseIf overMouse = True And downMouse = False Then
If Not _imgOver Is Nothing Then
e.Graphics.DrawImage(_imgOver, 0, 0)
Else : e.Graphics.DrawImage(_imgNorm, 0, 0)
End If
ElseIf overMouse = True And downMouse = True Then
If Not _imgDown Is Nothing Then
e.Graphics.DrawImage(_imgDown, 0, 0)
Else : e.Graphics.DrawImage(_imgNorm, 0, 0)
End If
Else : e.Graphics.DrawImage(_imgNorm, 0, 0)
End If
ElseIf Not MyBase.Enabled Then
e.Graphics.DrawImage(convert2Grayscale(_imgNorm), 0, 0)
End If
End If
If Not MyBase.Text = Nothing Then
'measure text
Dim txtSize As SizeF
txtSize = e.Graphics.MeasureString(MyBase.Text, MyBase.Font)
'set strFormat
Dim strFormat As New StringFormat()
Select Case MyBase.TextAlign
Case ContentAlignment.BottomLeft, ContentAlignment.MiddleLeft, ContentAlignment.TopLeft
strFormat.Alignment = StringAlignment.Near
Case ContentAlignment.BottomCenter, ContentAlignment.MiddleCenter, ContentAlignment.TopCenter
strFormat.Alignment = StringAlignment.Center
Case ContentAlignment.BottomRight, ContentAlignment.MiddleRight, ContentAlignment.TopRight
strFormat.Alignment = StringAlignment.Far
End Select
Dim rectF As RectangleF
rectF.X = 0
rectF.Width = MyBase.Width
rectF.Height = MyBase.Height
Dim txtLinesReqd, txtHeightReqd, txtWidth, i As Integer
Dim txtWidths() As String
If txtSize.Width > rectF.Width Then
txtLinesReqd = CInt(Math.Ceiling(txtSize.Width / rectF.Width))
txtHeightReqd = txtLinesReqd * txtSize.Height
txtWidths = Split(MyBase.Text, Environment.NewLine)
For i = 0 To txtWidths.GetUpperBound(0)
If txtWidths(i).Length > txtWidth Then
txtWidth = txtWidths(i).Length
End If
Next
Else
txtHeightReqd = txtSize.Height
txtWidth = txtsize.Width
End If
'find rectF.Y
Select Case MyBase.TextAlign
Case ContentAlignment.TopCenter, ContentAlignment.TopLeft, ContentAlignment.TopRight
rectF.Y = 0
Case ContentAlignment.MiddleCenter, ContentAlignment.MiddleLeft, ContentAlignment.MiddleRight
rectF.Y = CInt((MyBase.Height - txtHeightReqd) / 2)
Case ContentAlignment.BottomCenter, ContentAlignment.BottomLeft, ContentAlignment.BottomRight
rectF.Y = MyBase.Height - txtHeightReqd
End Select
Dim txtBrush As SolidBrush
If MyBase.Enabled Then
txtBrush = New SolidBrush(MyBase.ForeColor)
If overMouse = False And downMouse = False Then 'image norm
e.Graphics.DrawString(MyBase.Text, MyBase.Font, txtBrush, rectF, strFormat)
ElseIf overMouse = True And downMouse = False Then 'image over
e.Graphics.DrawString(MyBase.Text, MyBase.Font, txtBrush, rectF, strFormat)
ElseIf overMouse = True And downMouse = True Then 'image down
rectF.X += 1
rectF.Y += 1
e.Graphics.DrawString(MyBase.Text, MyBase.Font, txtBrush, rectF, strFormat)
End If
Else 'disabled gray image
txtBrush = New SolidBrush(Color.Gray)
e.Graphics.DrawString(MyBase.Text, MyBase.Font, txtBrush, rectF, strFormat)
End If
If Not txtBrush Is Nothing Then txtBrush.Dispose()
End If
End Sub
#End Region