Private m_Image As Image
Private m_BackColor As Color = SystemColors.Control
Public Property Image() As Image
Get
Return m_Image
End Get
Set(ByVal value As Image)
m_Image = value
End Set
End Property
Public Overrides Property BackColor() As Color
Get
Return m_BackColor
End Get
Set(ByVal value As Color)
m_BackColor = value
MyBase.Invalidate()
End Set
End Property
Private bPressed As Boolean
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
bPressed = True
MyBase.Invalidate()
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
bPressed = False
MyBase.Invalidate()
MyBase.OnMouseUp(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
If bPressed Then
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlDark), MyBase.ClientRectangle)
e.Graphics.DrawRectangle(New Pen(SystemColors.ControlDarkDark), 0, 0, MyBase.ClientSize.Width - 1, MyBase.ClientSize.Height - 1)
Else
e.Graphics.DrawRectangle(New System.Drawing.Pen(SystemColors.ControlDark), 0, 0, MyBase.ClientSize.Width - 1, MyBase.ClientSize.Height - 1)
End If
If m_Image IsNot Nothing Then
Dim ImgAttr As New Imaging.ImageAttributes()
ImgAttr.SetColorKey(Color.White, Color.White)
e.Graphics.DrawImage(m_Image, New Rectangle(CInt((MyBase.ClientSize.Width - m_Image.Width) / 2), CInt((MyBase.ClientSize.Height - m_Image.Height) / 2), m_Image.Width, m_Image.Height), 0, 0, m_Image.Width, m_Image.Height, GraphicsUnit.Pixel, ImgAttr)
End If
MyBase.OnPaint(e)
End Sub
Private Function SetColorAlpha(ByVal Color As Color, ByVal Alpha As Byte) As Color
Return Color.FromArgb(System.BitConverter.ToInt32(New Byte() {Color.B, Color.G, Color.R, Alpha}, 0))
End Function
Ajoute un fichier de code (vide) à ton projet et colle s'y le code.
Ensuite, tu compile ton projet puis dans la boîte à outils, tu auras un nouveau composant button que tu pourra insérer sur ta form.