Click here to Skip to main content
Click here to Skip to main content

Creating Glow Button

By , 24 Apr 2007
 

Screenshot - Demo.jpg

Introduction

This is my very first article to any open source site. Since I've gotten a lot useful article from this site, I'll be glad if I can to contribute to this great site.

This article will explain how to make a button that has a glow feature.

Background (optional)

When I installed Windows Vista, I was amazed by it's appearance. It had a very nice user interface and so many eye candy controls. That make me think about making something just like that. I've created some fancy controls that look like Vista components since that day. Also, I've always thought about making an article or tutorial to share my knowledge. Finally, while creating this glow button control, I've decided to make an article for it too.

So why Glow Button? I've been interested in this control since I played with Vista Windows, especially with its Minimize, Maximize and Close Buttons. They have some fancy glow affects. And thanks to .NET Framework 2.0, it's easier to create them.

Using the code

Basically, to create the glow effect, you have to make a "Layer" that will create the glow with GDI+. Unfortunately, .NET Framework 2.0 doesn't allow a control that can make other controls mid-transparent. Or rather, when you place a control above another control, you can't see the control below, even you set full transparency at the upper control. The trick for this button is to request from another button that surrounds it to make a glow layer for that control.

Since we want to ask other glow controls to make glow effects, we need a layer status for the 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

Now we have a status for the control to make layer as requested. If we must ask another surrounding button to make glow effect, how can we do that if the button is at the edge, side, top, bottom etc? For this problem, we need a special container for our control, so the control at edge or side can ask this container to make the glow effect. Let's name it "Grid".

Screenshot - Grid.jpg

Another feature that we will add is the ability to change themes dynamically. For this, we will use a colortable class called GlassColorTable and some defined colors for our control. Let's name it ColorSet. Also remember that a button has four default states, which is: Normal, Highlighted (Mouse Over), Pressed and Disabled. For the moment, I'm not including Disabled state, due to to the fact that I don't have enough time to experiment with it. I'll include it when I update my article.

Public Interface IGlassColor
    Enum States
        Normal = 0
        Highlighted = 1
        Pressed = 2
        Disabled = 3
    End Enum

    Sub NormalState()
    Sub HighlightState()
    Sub PressedState()
    Sub DisabledState()
End Interface

Public Class ColorSet
    'Normal State
    Public BackgroundHigh As Color
    '.
    '.
    '.
    Public TextColor As Color

    'Highlight State
    Public BackgroundHighFocus As Color
    '.
    '.
    '.
    Public TextColorFocus As Color

    'Glow Set
    Public GlowCenter As Color
End Class

Public Class GlassColorTable
    Implements IGlassColor

    Protected _BackgroundHigh As Color
    Protected _BackgroundLow As Color
    Protected _ShineHigh As Color
    Protected _ShineLow As Color
    Protected _BorderLeft As Color
    Protected _BorderRight As Color
    Protected _BorderTop As Color
    Protected _BorderBottom As Color
    Protected _TextColor As Color
    Protected _GlowCenter As Color

    Private _State As IGlassColor.States
    Private _cSet As ColorSet

    Public Overridable ReadOnly Property BackgroundLow() As Color
        Get
            Return _BackgroundHigh
        End Get
    End Property
    
    '.
    '.
    '.
End Class

To change themes dynamically, we must create a 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

Since we now have the ability to change themes for the button, we must let the button know which color we want to be layered from another control.

Screenshot - Themed.jpg

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

So, the most important part is done. It's time to use them for our application. Since we must ask help from another control to make the glow effect, we must set up in each control events which are "mouse enter" and "mouse leave" events.

Private Sub GlowButton1_MouseEnter(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles GlowButton1.MouseEnter
    GlowButton2.Layer = GlowingButton.GlowButton.LayerStatus.Left
    GlowButton4.Layer = GlowingButton.GlowButton.LayerStatus.Top
    GlowButton5.Layer = GlowingButton.GlowButton.LayerStatus.TopLeft

    GlowButton2.LayerColor = GlowButton1.LayerColor
    GlowButton4.LayerColor = GlowButton1.LayerColor
    GlowButton5.LayerColor = GlowButton1.LayerColor

    Grid1.GlowColor = GlowButton1.LayerColor
    Grid1.GlowStartPoint = New Point(GlowButton1.Location.X - 7, 
        GlowButton1.Location.Y - 7)
    Grid1.GlowStyle = GlowingButton.Grid.GlowStyles.TopLeft
End Sub

Private Sub GlowButton1_MouseLeave(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles GlowButton1.MouseLeave
    GlowButton2.Layer = GlowingButton.GlowButton.LayerStatus.None
    GlowButton4.Layer = GlowingButton.GlowButton.LayerStatus.None
    GlowButton5.Layer = GlowingButton.GlowButton.LayerStatus.None

    Grid1.GlowStyle = GlowingButton.Grid.GlowStyles.None
End Sub

To change themes dynamically, use the Renderer property.

For Each g As GlowingButton.GlowButton In Grid1.Controls
    g.Renderer = New GlowingButton.DiamondRed
Next

Points of Interest

Since we need to ask other controls to apply effects, we have to setup every control we have in the application. It will take time to setup everything, especially when we work with many buttons. To solve this problem, I suggest you make a class that inherits Grid control, place all glow buttons in the grid, and perform the required setup automatically with AddHandler methods. When I have more spare time, I'll explain in separate articles about this.

Additional notes about the demo: did some of you notice my other reason for creating this control? Yes, it's a Sudoku puzzle! My original motive with this control was for creating a Sudoku Puzzle Solver that can apply customized regions (that's why it needed themes or colorsets). The solver itself is still a long way to go because I've include it with many useful features.

That's all.

History

April 20th, 2007: Original Article.

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

About the Author

michaelrawi
Team Leader Component Crafts
Indonesia Indonesia
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey24 Feb '12 - 3:14 
Nice
GeneralIssue on button focus allowing text to be changedmemberMember 77736205 Jun '11 - 15:02 
Hi,
I am currently using your Glow Buttons to improve the GUI of a calculator of mine. I notice though that while the button has focus, if you click escape the text of the button is erased. And also while you have focus, you can click any key and it will replace the text with the corresponding key.
 
In short, after clicking and bringing focus to the button, any key pressed will replace the text with the key text.
 
How can I fix this?
 
Thanks,
 
Jordan from Meta's Development - metasdevelopment.com
GeneralGLOWING BUTTON IS NOT DECLARED !memberšprici21 Feb '10 - 2:51 
GLOWING BUTTON IS NOT DECLARED ! 100+ errors :(
GeneralRe: GLOWING BUTTON IS NOT DECLARED !memberMichael Rawi4 May '11 - 4:41 
Have you built the project before attach it on your application?
What are you read at ?

GeneralHelp mememberKhanh200727 Aug '07 - 4:25 
I want to make a close button like Windows Vista, but I don't know how to do that Confused | :confused: , can somebody help me?
GeneralError MessagememberEasy MAX25 Aug '07 - 3:51 
Assembly 'C:\Users\Matthew\Downloads\Visual Studio\Visual Studio 2005\Code Project Stuff\GlowingButtonDemo\GlowingButton\GlowingButton\obj\Debug\GlowingButton.dll' doesn't contain any UserControl types.
 
(Thats the message i got when i try to run it Frown | :(
GeneralRe: Error MessagememberMichael Rawi26 Aug '07 - 22:55 
After download, unzip and open the source, built solution first, then run the code. Alternatively you can use clean solution, then built and runt the code.
 
Regards
 
Michael Rawi
 
What are you read at ?

GeneralWont run for meememberEasy MAX25 Aug '07 - 3:48 
I cant get it to run,
 
ive downloaded source and demo twice and still it dosn't load up...
says at first its not trusted..
 
Frown | :(
GeneralWork´smemberLuzifer6664 May '07 - 4:07 
1. If you donwload demo project, make sure you select TestExecute project as startup
2. If you donwload the source, you have to add Windows Application project to use it because it's a Control Library project.
 
It work´s. Thanks
Big Grin | :-D
GeneralDon´t run too!memberLuzifer6663 May '07 - 23:03 
I couldn't get it to load - errors:
 
ArgumentException was Unhandled:
Could not find type 'GlowingButton.CalmCyan'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
 
1. Which version did you use ? Demo or source ?
Demo and source
2. What version of Visual Studio did you use ?
Visual Studio 2005 Proffesional
QuestionWouldn't runmemberDennis at ADS3 May '07 - 12:58 
I couldn't get it to load - errors:
Could not find type 'GlowingButton.CalmCyan'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.


 
-dennis

AnswerRe: Wouldn't runmemberMichael Rawi3 May '07 - 17:37 
1. Which version did you use ? Demo or source ?
2. What version of Visual Studio did you use ? This code made with Visual Studio 2005 Proffesional. I've re-download my source and demo project and both of the are sucessfully run in my computer.
 
What are you read at ?

GeneralRe: Wouldn't runmemberDennis at ADS4 May '07 - 5:20 
VS 2005. I tried both the demo and source with no luck.

 
-dennis

AnswerRe: Wouldn't runmemberDennis at ADS4 May '07 - 5:35 
I first received this error msg when loaded:
One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
 
Could not find type 'GlowingButton.CalmCyan'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.

 
But after I set the TestExecute as the start project. It worked.
Thanks.

 
-dennis

GeneralError while executing demomemberIreshPatel3 May '07 - 3:28 
Assembly 'C:\Temp\GlowingButtonDemo\GlowingButton\GlowingButton\obj\Debug\GlowingButton.dll' doesn't contain any UserControl types.
GeneralRe: Error while executing demomemberMichael Rawi3 May '07 - 17:31 
IreshPatel wrote:
Assembly 'C:\Temp\GlowingButtonDemo\GlowingButton\GlowingButton\obj\Debug\GlowingButton.dll' doesn't contain any UserControl types.

 
1. If you donwload demo project, make sure you select TestExecute project as startup
2. If you donwload the source, you have to add Windows Application project to use it because it's a Control Library project.
 
What are you read at ?

GeneralThat's awesome.member--=A J E E S H=--2 May '07 - 17:23 
That's awesome.
 
Regards,
--=A J E E S H=--

GeneralDemo program and TabmemberNchantim1 May '07 - 2:53 
The demo program seemed to respond to the tab key by removing the text on the glowing buttons.
GeneralRe: Demo program and TabmemberMichael Rawi1 May '07 - 15:51 
Nchantim wrote:
The demo program seemed to respond to the tab key by removing the text on the glowing buttons.

 
Sorry, I don't understand what you mean. Can you explain more details ?
 
What are you read at ?

GeneralRe: Demo program and TabmemberNchantim1 May '07 - 19:20 
Well, run the demo program, and hit the tab key about 20 times.... the numbers on the glowing buttons vanish, which is not expected behaviour.
GeneralRe: Demo program and TabmemberMichael Rawi1 May '07 - 19:38 
Nchantim wrote:
Well, run the demo program, and hit the tab key about 20 times.... the numbers on the glowing buttons vanish, which is not expected behaviour.

 
Oh, I see your point. This happened from Key_Up Overrides method. I directly send any keycode value to text property. I didn't put any spesific conditions in it and still don't have any idea what condition I want to put.
 
I'm preparing some updates on this control which hopefully can upload it this month, and I'll also try to improve it to solve this problem as well. Anyway, thanks for your feedback. It's greatly appreciated.
 
What are you read at ?

GeneralGrateful for interesting code in VBmemberserbach30 Apr '07 - 4:46 
Michael,
 
I program in VB and JavaScript every day for my employer. I can follow C# as a result of my work with JavaScript, but I'm always very pleased when somebody recognizes that VB is a perfectly good language for anything C# is used for. For that reason I use DotNetNuke for my web portal and I read every new article written using VB on The Code Project.
 
Just wanted to thank you for your interesting code and especially for writing it in VB.
 
Sincerely,
 
Steve Erbach
Neenah, WI
http://TheTownCrank.blogspot.com

GeneralRe: Grateful for interesting code in VBmemberMichael Rawi1 May '07 - 15:59 
You're welcome.
 
Yeah, I'm programming in VB since VB 6. While moving from VB6 to VB2005 a year ago, I decided to stay in my main programming language rather than migrating to C#. I found VB is more interesting than any other language.
 
What are you read at ?

Generalthanksmember-tusk-25 Apr '07 - 15:53 
"This is my very first article to any open source site. Since I've gotten a lot useful article from this site, I'll be glad if I can to contribute to this great site."
 

You are much welcome, and actually I think one day I'll do the same.
^^
 
I'm about to study your code Wink | ;) look very interesting !

GeneralRe: thanksmemberMichael Rawi25 Apr '07 - 17:26 
You're welcome.
 
My blog: http://vbcorner.blogspot.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 24 Apr 2007
Article Copyright 2007 by michaelrawi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid