Click here to Skip to main content
15,885,435 members
Articles / Multimedia / GDI+

Creating Glow Button

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
24 Apr 20073 min read 128.8K   5.8K   83  
An article of how to make a button that can glow
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing

''' <summary>
''' Represent a Glowing Button
''' </summary>
''' <remarks></remarks>
<Description("Represent a Glowing Button")> _
Public Class GlowButton
    Inherits Control

    Public Enum LayerStatus
        None = 0
        Self = 1
        TopLeft = 2
        Top = 3
        TopRight = 4
        Left = 5
        Right = 6
        BottomLeft = 7
        Bottom = 8
        BottomRight = 9
    End Enum

    Private _Layer As LayerStatus = LayerStatus.None
    Private _LayerColor As Color = Color.Transparent

    Private _Highlighted As Boolean = False

    Private _GlowLength As Integer = 8
    Private _ColorTable As New GlassColorTable

    <Browsable(False)> _
    Public Property Layer() As LayerStatus
        Get
            Return _Layer
        End Get
        Set(ByVal value As LayerStatus)
            _Layer = value

            Me.Invalidate()
        End Set
    End Property

    <Browsable(False)> _
    Public Property LayerColor() As Color
        Get
            Return _ColorTable.GlowCenter
        End Get
        Set(ByVal value As Color)
            _LayerColor = value
        End Set
    End Property

    <Browsable(False)> _
    Public Property Renderer() As ColorSet
        Get
            Return _ColorTable.Renderer
        End Get
        Set(ByVal value As ColorSet)
            _ColorTable.Renderer = value
            _ColorTable.State = IGlassColor.States.Normal
            Me.Refresh()
        End Set
    End Property

    Private Sub Initialize()
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or _
                    ControlStyles.SupportsTransparentBackColor Or ControlStyles.OptimizedDoubleBuffer Or _
                    ControlStyles.UserPaint, True)

        Me.Size = New Size(34, 34)
        Me.BackColor = Color.Transparent

        Me.Text = ""
        Me.Font = New System.Drawing.Font("Segoe UI", 20.25!, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))
    End Sub

    Sub New()
        Me.Initialize()
        Me._ColorTable.Renderer = New CalmCyan
        Me._ColorTable.State = IGlassColor.States.Normal
    End Sub

    Sub New(ByVal Renderer As ColorSet)
        Me.Initialize()
        Me._ColorTable.Renderer = Renderer
        Me._ColorTable.State = IGlassColor.States.Normal
    End Sub

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        MyBase.OnClick(e)
        Me.Select()
    End Sub

    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
        MyBase.OnGotFocus(e)
    End Sub

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyUp(e)
        Me.Text = Chr(e.KeyCode)
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
        MyBase.OnLostFocus(e)
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        Me._ColorTable.State = IGlassColor.States.Pressed

        Me.Refresh()
    End Sub

    Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
        MyBase.OnMouseEnter(e)
        Me._ColorTable.State = IGlassColor.States.Highlighted

        _Highlighted = True
        Me.BringToFront()
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        Me._ColorTable.State = IGlassColor.States.Normal

        _Highlighted = False
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)
        Me._ColorTable.State = IGlassColor.States.Highlighted

        Me.Refresh()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim BackRect As New Rectangle(1, Me.Height \ 2, Me.Width - 2, Me.Height \ 2 - 1)
        Dim ShineRect As New Rectangle(1, 1, Me.Width - 2, Me.Height \ 2 - 1)

        Using lgb As New Drawing2D.LinearGradientBrush(BackRect, _
                _ColorTable.BackgroundHigh, _ColorTable.BackgroundLow, Drawing2D.LinearGradientMode.Vertical)

            e.Graphics.FillRectangle(lgb, BackRect)
        End Using

        Using lgb2 As New Drawing2D.LinearGradientBrush(ShineRect, _
            _ColorTable.ShineHigh, _ColorTable.ShineLow, Drawing2D.LinearGradientMode.Vertical)

            e.Graphics.FillRectangle(lgb2, ShineRect)
        End Using

        e.Graphics.DrawLine(New Pen(_ColorTable.BorderTop), 1, 1, Me.Width - 2, 1)
        e.Graphics.DrawLine(New Pen(_ColorTable.BorderBottom), 1, Me.Height - 2, Me.Width - 2, Me.Height - 2)

        e.Graphics.DrawLine(New Pen(_ColorTable.BorderLeft), 1, 1, 1, Me.Height - 2)
        e.Graphics.DrawLine(New Pen(_ColorTable.BorderRight), Me.Width - 2, 1, Me.Width - 2, Me.Height - 3)

        e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
        e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(_ColorTable.TextColor), ((Me.Width - e.Graphics.MeasureString(Me.Text, Me.Font).Width) / 2), ((Me.Height - e.Graphics.MeasureString(Me.Text, Me.Font).Height) / 2) + 1)

        If _Highlighted Then
            e.Graphics.DrawRectangle(New Pen(_ColorTable.GlowCenter), 0, 0, Me.Width - 1, Me.Height - 1)
        End If

        'Here is the layer
        If _Layer <> LayerStatus.None Then
            Dim x As Integer = 0
            Dim y As Integer = 0

            Select Case _Layer
                Case LayerStatus.Bottom
                    y += 34
                Case LayerStatus.BottomLeft
                    x -= 34
                    y += 34
                Case LayerStatus.BottomRight
                    x += 34
                    y += 34
                Case LayerStatus.Left
                    x -= 34
                Case LayerStatus.Right
                    x += 34
                Case LayerStatus.Top
                    y -= 34
                Case LayerStatus.TopLeft
                    x -= 34
                    y -= 34
                Case LayerStatus.TopRight
                    x += 34
                    y -= 34
            End Select

            Dim StartPoint As New Point(x - _GlowLength, y - _GlowLength)

            Using pth As New Drawing2D.GraphicsPath
                pth.AddRectangle(New Rectangle(StartPoint, New Size(34 + _GlowLength * 2, 34 + _GlowLength * 2)))

                Using pgb As New Drawing2D.PathGradientBrush(pth)
                    'Normal
                    pgb.CenterColor = _LayerColor 'This is the real one
                    pgb.SurroundColors = New Color() {Color.Transparent}
                    pgb.FocusScales = New PointF(0.7F, 0.7F)

                    'Color Blend
                    'Dim cb As New Drawing2D.ColorBlend(2)
                    'cb.Positions(0) = 0.0F
                    'cb.Positions(1) = 1.0F

                    'cb.Colors = New Color() {Color.Transparent, Color.FromArgb(150, 93, 198, 241)}

                    'pgb.InterpolationColors = cb
                    'pgb.FocusScales = New PointF(0.7F, 0.7F)

                    e.Graphics.FillPath(pgb, pth)
                End Using
            End Using
        End If
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader Component Crafts
Indonesia Indonesia
Got his BA in Information and Technology from University of Surabaya, Indonesia in 2004. Finished his post graduate at Magistrate and Management Institute of Technology, Surabaya in 2009.

He has developed several middle to large scale enterprise application, mostly on windows based architecture.

Currently working as IT Manager on a company based on Sidoarjo.

Comments and Discussions