Click here to Skip to main content
15,888,113 members
Articles / Programming Languages / XML

Creating a Glass Button using GDI+

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

Screenshots

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.

Introduction

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:

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

  <Border.Background>
    <LinearGradientBrush EndPoint="0.494,0.889" 
                         StartPoint="0.494,0.028">
      <GradientStop Color="#99FFFFFF" Offset="0" />

      <GradientStop Color="#33FFFFFF" Offset="1" />
    </LinearGradientBrush>
  </Border.Background>

</Border>

into:

C#
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),
                          LinearGradientMode.Vertical))
  {
    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.

History

  • 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

License

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


Written By
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Science and Technology, Poland.

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

Comments and Discussions

 
AnswerRe: Can I use this? Pin
Lukasz Sw.2-Mar-07 5:42
Lukasz Sw.2-Mar-07 5:42 
GeneralCool stuff Pin
woposmail28-Feb-07 10:10
woposmail28-Feb-07 10:10 
GeneralRe: Cool stuff Pin
Lukasz Sw.28-Feb-07 10:18
Lukasz Sw.28-Feb-07 10:18 
GeneralPerformance Pin
H_R_O28-Feb-07 1:52
H_R_O28-Feb-07 1:52 
GeneralNice work, 1 question Pin
Michael Rawi27-Feb-07 15:44
Michael Rawi27-Feb-07 15:44 
GeneralRe: Nice work, 1 question Pin
Lukasz Sw.28-Feb-07 2:38
Lukasz Sw.28-Feb-07 2:38 
GeneralRe: Nice work, 1 question Pin
Michael Rawi28-Feb-07 20:08
Michael Rawi28-Feb-07 20:08 
QuestionVerry slow if more than one button is used Pin
H_R_O27-Feb-07 1:30
H_R_O27-Feb-07 1:30 
Hi,

i have check the control, it looks funny.Cool | :cool:

But if you have 24 Glass-Buttons on the form, than the performance is gowing down rapid.Frown | :( zzz:

Is this because every button uses a Timer-Thread?.

Redrawing of many buttons let the form and the buttons flicker, you can see
how every button is drawn...

The solution could be to hold all buttons in one panel with one timer and draw all together
depending on the clipp rectangle of the paint event of the panel. The buttons themself do not paint directly, they paint on the gc from the panel...?

Roman

AnswerRe: Verry slow if more than one button is used Pin
Ernest Laurentin27-Feb-07 5:19
Ernest Laurentin27-Feb-07 5:19 
AnswerRe: Verry slow if more than one button is used Pin
Patrick Etc.27-Feb-07 6:08
Patrick Etc.27-Feb-07 6:08 
AnswerRe: Verry slow if more than one button is used Pin
Lukasz Sw.27-Feb-07 8:59
Lukasz Sw.27-Feb-07 8:59 
GeneralGreat! Pin
Albert Dadze23-Feb-07 6:34
Albert Dadze23-Feb-07 6:34 
GeneralRe: Great! Pin
Lukasz Sw.23-Feb-07 7:10
Lukasz Sw.23-Feb-07 7:10 
GeneralVery nice Pin
FoxholeWilly22-Feb-07 3:45
FoxholeWilly22-Feb-07 3:45 
GeneralRe: Very nice Pin
Lukasz Sw.22-Feb-07 7:08
Lukasz Sw.22-Feb-07 7:08 
GeneralQuick question Pin
TemplMetaProg22-Feb-07 1:33
TemplMetaProg22-Feb-07 1:33 
GeneralRe: Quick question Pin
Lukasz Sw.22-Feb-07 2:51
Lukasz Sw.22-Feb-07 2:51 
GeneralRe: Quick question Pin
Patrick Etc.23-Feb-07 8:56
Patrick Etc.23-Feb-07 8:56 
GeneralRe: Quick question Pin
Lukasz Sw.23-Feb-07 15:08
Lukasz Sw.23-Feb-07 15:08 
GeneralTextAlign Property didnt work... Pin
yarns22-Feb-07 1:07
yarns22-Feb-07 1:07 
GeneralRe: TextAlign Property didnt work... Pin
Lukasz Sw.22-Feb-07 2:28
Lukasz Sw.22-Feb-07 2:28 
GeneralVS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
knvb112321-Feb-07 17:30
knvb112321-Feb-07 17:30 
GeneralRe: VS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
Lukasz Sw.21-Feb-07 23:07
Lukasz Sw.21-Feb-07 23:07 
GeneralRe: VS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
knvb112322-Feb-07 13:24
knvb112322-Feb-07 13:24 
GeneralRe: VS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
gurumosch22-Feb-07 23:14
gurumosch22-Feb-07 23:14 

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.