Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Public Class BotonPersonalizado : Inherits UserControl
#Region " [ ### CONSTRUCTOR ### ] "
Public Sub New()
With Me
.SuspendLayout()
.BackColor = Color.Transparent
.Cursor = Cursors.Hand
.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular, GraphicsUnit.Point)
.ForeColor = Color.White
.MinimumSize = New Size(25, 20)
.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
.SetStyle(ControlStyles.ResizeRedraw, True)
.SetStyle(ControlStyles.Opaque, False)
.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
.SetStyle(ControlStyles.Selectable, True)
.Size = New Size(125, 28)
.TabStop = True
.Texto = "Personal Button"
.ResumeLayout()
End With
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
params.ExStyle = &H20
Return params
End Get
End Property
#End Region
#Region " [ ### PROPIEDADES ### ] "
Private Color1 As Color = Color.MediumSeaGreen
<Category("Degradado"), Description("Especifica el color primario del fondo degradado.")>
Public Property ColorPrimario() As Color
Get
Return Color1
End Get
Set(ByVal value As Color)
Color1 = value
Me.Invalidate()
End Set
End Property
Private Color2 As Color = Color.FromArgb(33, 38, 34)
<Category("Degradado"), Description("Especifica el color secundario del fondo degradado.")>
Public Property ColorSecundario() As Color
Get
Return Color2
End Get
Set(ByVal value As Color)
Color2 = value
Me.Invalidate()
End Set
End Property
Private ColorOver1 As Color = Color.Goldenrod
<Category("Degradado"), Description("Especifica el color que usara el fondo degradado para el efecto hover.")>
Public Property ColorHover() As Color
Get
Return ColorOver1
End Get
Set(ByVal value As Color)
ColorOver1 = value
Me.Invalidate()
End Set
End Property
Private Text0 As String
<Category("Appearance"), Description("Especifica el texto que mostrara el control.")>
Public Property Texto() As String
Get
Return Text0
End Get
Set(ByVal value As String)
Text0 = value
Me.Invalidate()
End Set
End Property
#End Region
#Region " [ ### DIBUJADO ### ] "
' Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Private Sub DibujarBoton(ByVal colorA As Color, ByVal colorB As Color)
Dim g As Graphics = Me.CreateGraphics
g.InterpolationMode = InterpolationMode.HighQualityBilinear
g.CompositingQuality = CompositingQuality.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
g.SmoothingMode = SmoothingMode.AntiAlias
Dim brush As New LinearGradientBrush(Me.ClientRectangle, colorA, colorB, 90, True)
Using gp As New GraphicsPath()
AddRoundedRectangle(gp, New Point(1, 1), New Size(Me.Size.Width - 2, Me.Size.Height - 2))
g.FillPath(brush, gp)
End Using
'Using gp As New GraphicsPath()
' AddRoundedRectangle(gp, New Point(0, 0), Me.Size)
' Me.Region = New Region(gp) ' <- TAMBIEN SE PUEDE USAR ASI.
'End Using
' MyBase.OnPaint(Me.CreateGraphics)
Using gText As Graphics = Me.CreateGraphics
Dim TextSize As SizeF = gText.MeasureString(Text0, Me.Font)
gText.DrawString(Text0, Me.Font, New SolidBrush(Me.ForeColor), (Me.Width / 2) - (TextSize.Width / 2), (Me.Height / 2) - (TextSize.Height / 2))
End Using
brush.Dispose()
g.Dispose()
End Sub
Private Sub AddRoundedRectangle(ByVal gp As GraphicsPath, ByVal upperLeft As Point, ByVal size As Size)
gp.AddArc(New Rectangle(upperLeft, New Size(size.Height, size.Height)), 90, 180)
gp.AddLine(New Point(size.Height / 2, upperLeft.Y), New Point(size.Width - (size.Height / 2), upperLeft.Y))
gp.AddArc(New Rectangle(New Point(size.Width - size.Height, upperLeft.Y), New Size(size.Height, size.Height)), -90, 180)
gp.CloseFigure()
End Sub
#End Region
Private Sub BotonPersonalizado_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
MsgBox("Esta porqueria funciona!")
End Sub
Private Sub BotonPersonalizado_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
DibujarBoton(ColorOver1, Color2)
End Sub
Private Sub BotonPersonalizado_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
DibujarBoton(Color1, Color2)
End Sub
Private Sub BotonPersonalizado_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
DibujarBoton(Color1, Color2)
End Sub
End Class