Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms.Design
<Designer(GetType(BitmapButton.BitmapButtonDesigner))>
Public Class BitmapButton
Inherits PictureBox
Implements IButtonControl, INotifyPropertyChanged
#Region "Field"
Private _dialogResult As DialogResult
Private _buttonState As ButtonState = ButtonState.None
' من الأأفضل هنا اضافة الصور للمشروع و اعطاؤها قيمة أولية لتجنب الأخطاء في تصميم الكونترول
Private _defaultImage As Image = CType(Nothing, Image)
Private _mousedownImage As Image = CType(Nothing, Image)
Private _mouseoverImage As Image = CType(Nothing, Image)
#End Region
#Region "Constructor"
Public Sub New()
DoubleBuffered = True
BackColor = Color.Transparent
SizeMode = PictureBoxSizeMode.AutoSize
End Sub
#End Region
#Region "Property"
Protected Overrides ReadOnly Property DefaultSize As Size
Get
Return New Size(100, 25)
End Get
End Property
Public Property DialogResult As DialogResult Implements IButtonControl.DialogResult
Get
Return _dialogResult
End Get
Set(value As DialogResult)
_dialogResult = value
End Set
End Property
Private Property State() As ButtonState
Get
Return _buttonState
End Get
Set(value As ButtonState)
_buttonState = value
OnPropertyChanged()
End Set
End Property
Public Property DefaultImage As Image
Get
Return _defaultImage
End Get
Set(value As Image)
_defaultImage = value
OnPropertyChanged()
End Set
End Property
Public Property MousedownImage() As Image
Get
Return _mousedownImage
End Get
Set(value As Image)
_mousedownImage = value
OnPropertyChanged()
End Set
End Property
Public Property MouseoverImage() As Image
Get
Return _mouseoverImage
End Get
Set(value As Image)
_mouseoverImage = value
OnPropertyChanged()
End Set
End Property
<Browsable(True)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
<Category("Appearance")>
<Description("Gets or sets the control text.")>
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(value As String)
MyBase.Text = value
End Set
End Property
<Browsable(True)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
<Category("Appearance")>
<Description("Gets or sets the font of the control text.")>
Public Overrides Property Font As Font
Get
Return MyBase.Font
End Get
Set(value As Font)
MyBase.Font = value
End Set
End Property
<Browsable(False)>
<EditorBrowsable(EditorBrowsableState.Never)>
<Category("Appearance")>
<Description(" Gets or sets the image of the control.")>
Public Shadows Property Image() As Image
Get
Return MyBase.Image
End Get
Set(value As Image)
MyBase.Image = value
End Set
End Property
#End Region
#Region "Method"
Public Sub NotifyDefault(value As Boolean) Implements IButtonControl.NotifyDefault
State = If(value, State Or ButtonState.Default, State And Not ButtonState.[Default])
End Sub
Public Sub PerformClick() Implements IButtonControl.PerformClick
If CanSelect Then
OnClick(EventArgs.Empty)
End If
End Sub
Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional propertyName As [String] = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
If propertyName = "DefaultImag" Then
Image = DefaultImage
End If
End Sub
Protected Overrides Sub OnMouseLeave(e As EventArgs)
MyBase.OnMouseLeave(e)
State = State And Not ButtonState.Mouseover
Image = DefaultImage
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
State = State Or ButtonState.Mousedown
Image = MousedownImage
End Sub
Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
MyBase.OnMouseUp(e)
State = State And Not ButtonState.Mousedown
Image = If(((State And ButtonState.Mouseover) <> 0), MouseoverImage, _defaultImage)
End Sub
Protected Overrides Sub OnMouseEnter(e As EventArgs)
MyBase.OnMouseEnter(e)
State = State Or ButtonState.Mouseover
Image = MouseoverImage
End Sub
Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
Refresh()
End Sub
Protected Overrides Sub OnClick(e As EventArgs)
Dim currentOwner = Me.FindForm()
If currentOwner IsNot Nothing Then
currentOwner.DialogResult = _dialogResult
End If
MyBase.OnClick(e)
End Sub
Protected Overrides Sub OnPaint(pe As PaintEventArgs)
MyBase.OnPaint(pe)
If (Not String.IsNullOrEmpty(Text)) AndAlso (pe IsNot Nothing) AndAlso (MyBase.Font IsNot Nothing) Then
Dim sb As New SolidBrush(MyBase.ForeColor)
Dim textSize As SizeF = pe.Graphics.MeasureString(MyBase.Text, MyBase.Font)
Dim pt As PointF = CType(Nothing, PointF)
If MyBase.Image IsNot Nothing Then
pt = New PointF(MyBase.Image.Width / 2 - textSize.Width / 2, MyBase.Image.Height / 2 - textSize.Height / 2)
Else
pt = New PointF(MyBase.Width / 2 - textSize.Width / 2, MyBase.Height / 2 - textSize.Height / 2)
End If
pe.Graphics.DrawString(MyBase.Text, MyBase.Font, sb, pt)
End If
End Sub
#End Region
#Region " Event"
<Description("Occurs when a property value changes.")> <Category("Property Changed")>
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
#End Region
#Region "ButtonStates"
Private Enum ButtonState
None
[Default]
Mouseover
Mousedown
End Enum
#End Region
#Region "BitmapButtonDesigner"
' هذا الكلاس الهدف منه تغيير شكل البكتشربوكس الأصلي في مرحلة التصميم وذلك بعد تحويله الي باتون
Friend Class BitmapButtonDesigner
Inherits ControlDesigner
Protected Overrides Sub PostFilterAttributes(attributes As IDictionary)
MyBase.PostFilterAttributes(attributes)
Dim attr As Attribute = New DockingAttribute(DockingBehavior.Never)
attributes(GetType(DockingAttribute)) = attr
End Sub
Public Overrides ReadOnly Property SelectionRules() As SelectionRules
Get
Return SelectionRules.Moveable
End Get
End Property
End Class
#End Region
End Class