Hover Button Control

a_jam_sandwich

Well-known member
Joined
Dec 10, 2002
Messages
367
Location
Uk
Code:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Data
Imports System.Windows.Forms

Public Class HoverButton : Inherits System.Windows.Forms.UserControl

    Private isHover As Boolean = False
    Private _offImage As Bitmap
    Private _OnImage As Bitmap
    Private _DisabledImage As Bitmap

     declare Off screen image and Offscreen graphics       
    Private OffScreenImage As Image
    Private gOffScreen As Graphics

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        This call is required by the Windows Form Designer.
        InitializeComponent()

        Add any initialization after the InitializeComponent() call

    End Sub

    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()
        
        HoverButton
        
        Me.Name = "HoverButton"
        Me.Size = New System.Drawing.Size(64, 64)

    End Sub

#End Region

    Public Property offImage() As Image
        Get
            Return _offImage
        End Get
        Set(ByVal Value As Image)
            _offImage = Value
            Invalidate()
        End Set
    End Property

    Public Property onImage() As Image
        Get
            Return _OnImage
        End Get
        Set(ByVal Value As Image)
            _OnImage = Value
            Invalidate()
        End Set
    End Property

    Public Property disabledImage() As Image
        Get
            Return _DisabledImage
        End Get
        Set(ByVal Value As Image)
            _DisabledImage = Value
            Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        If (_offImage Is Nothing) Or (_OnImage Is Nothing) Then Exit Sub
        Dim g As Graphics = e.Graphics
        g.Clear(Me.BackColor)
        If Me.Enabled = True Then
            If isHover = True Then
                g.DrawImage(_OnImage, 0, 0, ClientRectangle.Width, ClientRectangle.Height)
            Else
                g.DrawImage(_offImage, 0, 0, ClientRectangle.Width, ClientRectangle.Height)
            End If
        Else
            g.DrawImage(_DisabledImage, 0, 0, ClientRectangle.Width, ClientRectangle.Height)
        End If
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
        Me.isHover = False
        Me.Refresh()
        MyBase.OnMouseLeave(New EventArgs())
    End Sub

    Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs)
        Me.isHover = True
        Me.Refresh()
        MyBase.OnMouseEnter(New EventArgs())
    End Sub

A hover button control for anyone who wants it 3 states On, Off, Disabled maybe you may want to back buffer if so post and ill add

Andy
 
Last edited by a moderator:
Back
Top