Click here to Skip to main content
15,391,695 members
Articles / Programming Languages / XML
Posted 19 Feb 2007


610 bookmarked

Creating a Glass Button using GDI+

Rate me:
Please Sign up or sign in to vote.
4.86/5 (216 votes)
26 Mar 2013CPL2 min read
How to create an animating glass button using only GDI+ (and not using WPF)


Sample application using a standard glass button with image.

Sample application using a standard glass button with image.

The same application, but this time it has a customized glass button.

Sample application using a customized glass button.

MFC application which hosts four glass buttons.

MFC application which hosts four glass buttons.


I bet you have already seen animated task buttons in Windows Vista. I have. I was wondering how to create a similar control. Fortunately, I found a web page which describes how to do that using the Microsoft Expression Blend (Creating a Glass Button: The Complete Tutorial). The glass button (and thus the whole application) created with the Microsoft Expression Blend requires .NET Framework 3.0 to run. Because some people cannot or do not want to use .NET Framework 3.0 yet, I have decided to rewrite that cool control using only GDI+ so it would work with .NET Framework 2.0.

"Converting" XAML to C# (GDI+)

The tutorial from the page mentioned above was easy to complete, and the generated XAML code was so understandable that there were no big issues with a "conversion."

For example, I have translated the following code:

<Border HorizontalAlignment="Stretch" 
        Margin="0,0,0,0" x:Name="shine" 
        Width="Auto" CornerRadius="4,4,0,0">

    <LinearGradientBrush EndPoint="0.494,0.889" 
      <GradientStop Color="#99FFFFFF" Offset="0" />

      <GradientStop Color="#33FFFFFF" Offset="1" />



using (GraphicsPath bh = CreateTopRoundRectangle(rect2, 4))
  int opacity = 0x99;
  if (isPressed) opacity = (int)(.4f * opacity + .5f);
  using (Brush br = new LinearGradientBrush(rect2, 
                          Color.FromArgb(opacity, shineColor),
                          Color.FromArgb(opacity / 3, shineColor),
    g.FillPath(br, bh);

(This is only a fragment of the DrawButtonBackground method.)

Even the animation of a hovered button was easily obtained by using the Timer class. Unfortunately, an animation is not quite smooth when a glass button is quite big.

How to Use the GlassButton Class?

The GlassButton class derives from the Button class so it can be used in the same way. Displaying an image on a glass button is also supported now. Even the guidelines work fine in the Visual Studio's form designer.


  • 1.3.2 (02.11.2008) — Important! This is the last “standalone” version of the control. The next version will be included in a new project hosted at CodePlex.
    • Fixed a bug that caused the button's image to be disposed in certain situations
  • 1.3.1 (27.10.2008)
    • The source code is now available both in C# and VB.NET
    • Minor bugs fixed
  • 1.3 (19.11.2007)
    • The image is grayed when the button is disabled
    • Added property 'FadeOnFocus'
    • Improved performance
    • Minor bugs fixed
  • 1.2 (31.03.2007)
    • The 'disabled' look differs from the 'enabled' one
    • Added some 'PropertyChange' events
    • Improved performance
    • Split source code from compiled library and demo application
    • Added MFC demo application
    • Added toolbox bitmap
    • Minor bugs fixed
  • 1.1.1 (22.02.2007)
    • Minor bugs fixed
  • 1.1 (21.02.2007)
    • Added images support
  • 1.0 (19.02.2007)
    • First version


This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


About the Author

Lukasz Swiatkowski
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Science and Technology, Poland.

My interests: reading, programming, drawing, Japan, Spain.

Comments and Discussions

QuestionButton Image Pin
Giovanni Pietroluongo22-Jan-21 1:58
MemberGiovanni Pietroluongo22-Jan-21 1:58 
PraiseNice Pin
User 1327559323-Sep-17 3:55
MemberUser 1327559323-Sep-17 3:55 
QuestionHow to import in my project? Pin
Member 1099147112-Jan-15 5:23
MemberMember 1099147112-Jan-15 5:23 
AnswerRe: How to import in my project? Pin
Lukasz Swiatkowski13-Jan-15 21:18
MemberLukasz Swiatkowski13-Jan-15 21:18 
SuggestionHave 5 from me Pin
MarkBoreham7-Jan-14 21:58
professionalMarkBoreham7-Jan-14 21:58 
QuestionC++ / MFC Pin
Andy Bantly17-Apr-13 5:27
MemberAndy Bantly17-Apr-13 5:27 
AnswerRe: C++ / MFC Pin
Chris Mayyer22-May-13 23:34
MemberChris Mayyer22-May-13 23:34 
GeneralRe: C++ / MFC Pin
Andy Bantly23-May-13 1:31
MemberAndy Bantly23-May-13 1:31 
Buggood conversion from xaml to c# Pin
sridhar_thota27-Mar-13 0:04
Membersridhar_thota27-Mar-13 0:04 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 8:36
professionalKanasz Robert27-Sep-12 8:36 
QuestionGreat tool but unreadable code Pin
raminxtar17-Sep-12 1:46
Memberraminxtar17-Sep-12 1:46 
AnswerRe: Great tool but unreadable code Pin
Lukasz Swiatkowski17-Sep-12 1:49
MemberLukasz Swiatkowski17-Sep-12 1:49 
QuestionA great article Pin
Michael Haephrati1-Jul-12 7:15
mvaMichael Haephrati1-Jul-12 7:15 
GeneralMy vote of 5 Pin
Kraeven1-May-12 23:58
MemberKraeven1-May-12 23:58 
QuestionSometimes I get a red cross (red X) Pin
C#Bojo13-Mar-12 0:11
MemberC#Bojo13-Mar-12 0:11 
Questionniiiiiiiiiiiiice Pin
Des1re27-Feb-12 8:58
MemberDes1re27-Feb-12 8:58 
Generalvery goooooooooooooood ! Pin
kingtak25-Feb-12 22:47
Memberkingtak25-Feb-12 22:47 
Questionhow did you change form colour to black Pin
samit198425-Nov-11 0:06
Membersamit198425-Nov-11 0:06 
AnswerRe: how did you change form colour to black Pin
Lukasz Swiatkowski25-Nov-11 0:42
MemberLukasz Swiatkowski25-Nov-11 0:42 
QuestionHow to set image in MFC using resource Icons? Pin
Chandrak Baxi10-Apr-11 20:19
MemberChandrak Baxi10-Apr-11 20:19 
AnswerRe: How to set image in MFC using resource Icons? Pin
Lukasz Swiatkowski10-Apr-11 21:09
MemberLukasz Swiatkowski10-Apr-11 21:09 
GeneralMy vote of 5 Pin
drummerboy051124-Mar-11 9:39
professionaldrummerboy051124-Mar-11 9:39 
A very nice control!
GeneralMy vote of 5 Pin
csrss21-Dec-10 9:02
Membercsrss21-Dec-10 9:02 
GeneralBrilliant :) Pin
Anthony Daly3-Sep-10 11:21
MemberAnthony Daly3-Sep-10 11:21 
GeneralMy vote of 5 Pin
lastrebel2-Sep-10 13:24
Memberlastrebel2-Sep-10 13:24 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.