Public Class ButtonEx
    Inherits System.Windows.Forms.UserControl

#Region " Código generado por el Diseñador de Windows Forms "

    Public Sub New()
        MyBase.New()

        'El Diseñador de Windows Forms requiere esta llamada.
        InitializeComponent()

        'Agregar cualquier inicialización después de la llamada a InitializeComponent()

        SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)

    End Sub

    'UserControl1 reemplaza a Dispose para limpiar la lista de componentes.
    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

    'Requerido por el Diseñador de Windows Forms
    Private components As System.ComponentModel.IContainer

    'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
    'Puede modificarse utilizando el Diseñador de Windows Forms. 
    'No lo modifique con el editor de código.
    Friend WithEvents pMask As System.Windows.Forms.PictureBox
    Friend WithEvents pNormal As System.Windows.Forms.PictureBox
    Friend WithEvents pOver As System.Windows.Forms.PictureBox
    Friend WithEvents pPress As System.Windows.Forms.PictureBox
    Friend WithEvents ButtonStatesImages As System.Windows.Forms.ImageList
    Friend WithEvents pDisabled As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.pMask = New System.Windows.Forms.PictureBox()
        Me.pNormal = New System.Windows.Forms.PictureBox()
        Me.pOver = New System.Windows.Forms.PictureBox()
        Me.pPress = New System.Windows.Forms.PictureBox()
        Me.ButtonStatesImages = New System.Windows.Forms.ImageList(Me.components)
        Me.pDisabled = New System.Windows.Forms.PictureBox()
        Me.SuspendLayout()
        '
        'pMask
        '
        Me.pMask.BackColor = System.Drawing.Color.Transparent
        Me.pMask.Cursor = System.Windows.Forms.Cursors.Hand
        Me.pMask.Name = "pMask"
        Me.pMask.Size = New System.Drawing.Size(25, 25)
        Me.pMask.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.pMask.TabIndex = 0
        Me.pMask.TabStop = False
        '
        'pNormal
        '
        Me.pNormal.BackColor = System.Drawing.Color.Transparent
        Me.pNormal.Name = "pNormal"
        Me.pNormal.Size = New System.Drawing.Size(25, 25)
        Me.pNormal.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.pNormal.TabIndex = 1
        Me.pNormal.TabStop = False
        '
        'pOver
        '
        Me.pOver.BackColor = System.Drawing.Color.Transparent
        Me.pOver.Name = "pOver"
        Me.pOver.Size = New System.Drawing.Size(25, 25)
        Me.pOver.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.pOver.TabIndex = 2
        Me.pOver.TabStop = False
        '
        'pPress
        '
        Me.pPress.BackColor = System.Drawing.Color.Transparent
        Me.pPress.Name = "pPress"
        Me.pPress.Size = New System.Drawing.Size(25, 25)
        Me.pPress.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.pPress.TabIndex = 3
        Me.pPress.TabStop = False
        '
        'ButtonStatesImages
        '
        Me.ButtonStatesImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit
        Me.ButtonStatesImages.ImageSize = New System.Drawing.Size(25, 25)
        Me.ButtonStatesImages.TransparentColor = System.Drawing.Color.Transparent
        '
        'pDisabled
        '
        Me.pDisabled.BackColor = System.Drawing.Color.Transparent
        Me.pDisabled.Name = "pDisabled"
        Me.pDisabled.Size = New System.Drawing.Size(25, 25)
        Me.pDisabled.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.pDisabled.TabIndex = 4
        Me.pDisabled.TabStop = False
        '
        'ButtonEx
        '
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pMask, Me.pOver, Me.pNormal, Me.pPress, Me.pDisabled})
        Me.Cursor = System.Windows.Forms.Cursors.Hand
        Me.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Me.Name = "ButtonEx"
        Me.Size = New System.Drawing.Size(32, 32)
        Me.ResumeLayout(False)

    End Sub

#End Region

    'Máscara
    Private imgMask As Image

    'Determina si el control lanza los eventos
    Private _RaiseEvents As Boolean = True

    'Area activa
    Private Area As New ArrayList()

    'Cuando se presiona el botón
    Private Pressed As Boolean = False


    Public Property MaskImage() As Image

        Get

            Return imgMask

        End Get

        Set(ByVal Value As Image)

            imgMask = Value

            SetProperties()

        End Set

    End Property


    Public Property NormalImage() As Image

        Get

            Return pNormal.Image

        End Get

        Set(ByVal Value As Image)

            pNormal.Image = Value

            Me.pMask.Image = pNormal.Image

        End Set

    End Property


    Public Property OverImage() As Image

        Get

            Return pOver.Image

        End Get

        Set(ByVal Value As Image)

            pOver.Image = Value

        End Set

    End Property


    Public Property PressImage() As Image

        Get

            Return pPress.Image

        End Get

        Set(ByVal Value As Image)

            pPress.Image = Value

        End Set

    End Property


    Public Property DisabledImage() As Image

        Get

            Return pDisabled.Image

        End Get

        Set(ByVal Value As Image)

            pDisabled.Image = Value

        End Set

    End Property


    Public Sub SetProperties()

        Dim bmpArea As New Bitmap(imgMask)
        Dim Black As Color                           'Defino el color de la mascara
        Dim PixelArea As Color                       'Color del area del botón

        Dim I, J As Integer
        Dim WidthLong, HeightLong As Integer

        Black = Color.FromArgb(255, 0, 0, 0)

        Me.Width = imgMask.Width
        Me.Height = imgMask.Height

        WidthLong = Me.Width
        HeightLong = Me.Height

        For I = 1 To HeightLong - 1

            For J = 1 To WidthLong - 1

                PixelArea = bmpArea.GetPixel(J, I)

                If PixelArea.ToArgb = Black.ToArgb Then

                    Area.Add(New Point(J, I))

                End If

            Next

        Next

    End Sub


    Public Sub Enable()

        _RaiseEvents = True

        Me.pMask.Cursor = Cursors.Hand

        Me.pMask.Image = Me.pNormal.Image

    End Sub


    Public Sub Disable()

        _RaiseEvents = False

        Me.pMask.Cursor = Cursors.Arrow

        Me.pMask.Image = Me.pDisabled.Image

    End Sub


    Private Sub _MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pMask.MouseMove

        If _RaiseEvents Then

            Dim MousePoint As Point = New Point(e.X, e.Y)

            If Not Area.IndexOf(MousePoint) < 0 Then

                If Pressed Then

                    Me.pMask.Image = pPress.Image

                Else

                    Me.pMask.Image = pOver.Image

                End If

            End If

        End If

    End Sub


    Private Sub _MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pMask.MouseDown

        If _RaiseEvents Then

            Dim MousePoint As Point = New Point(e.X, e.Y)

            If Not Area.IndexOf(MousePoint) < 0 Then

                Me.pMask.Image = pPress.Image

                Pressed = True

                RaiseEvent MouseDown(Me, New MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, e.Delta))

            End If

        End If

    End Sub


    Private Sub _MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pMask.MouseUp

        If _RaiseEvents Then

            RaiseEvent MouseUp(sender, e)

            Me.pMask.Image = pOver.Image

            Pressed = False

        End If

    End Sub


    Private Sub _MouseEnter(ByVal sender As System.Object, ByVal e As EventArgs) Handles pMask.MouseEnter

        If _RaiseEvents Then

            RaiseEvent MouseEnter(sender, e)

        End If

    End Sub


    Private Sub _MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pMask.MouseLeave

        If _RaiseEvents Then

            Me.pMask.Image = pNormal.Image

        End If

    End Sub


    Public Shadows Event MouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs)


    Public Shadows Event MouseEnter(ByVal sender As System.Object, ByVal e As EventArgs)


    Public Shadows Event MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs)


End Class