Click here to Skip to main content
15,892,643 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 927.2K   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

 
GeneralRe: Paint event Pin
Mikael Bager17-Apr-07 19:50
Mikael Bager17-Apr-07 19:50 
GeneralRe: Paint event Pin
Lukasz Sw.18-Apr-07 2:49
Lukasz Sw.18-Apr-07 2:49 
GeneralI like it :-) Pin
Mc Gwyn3-Apr-07 7:07
Mc Gwyn3-Apr-07 7:07 
GeneralRe: I like it :-) Pin
Lukasz Sw.3-Apr-07 12:06
Lukasz Sw.3-Apr-07 12:06 
GeneralRe: I like it :-) Pin
Mc Gwyn4-Apr-07 20:44
Mc Gwyn4-Apr-07 20:44 
GeneralRe: Im a bit dubious about this [modified] Pin
Sacha Barber28-Mar-07 23:55
Sacha Barber28-Mar-07 23:55 
GeneralRe: Im a bit dubious about this Pin
Marcos Meli29-Mar-07 4:22
Marcos Meli29-Mar-07 4:22 
GeneralRe: Im a bit dubious about this Pin
Sacha Barber29-Mar-07 22:05
Sacha Barber29-Mar-07 22:05 
Marcos Meli wrote:
But like I said the most of the people some times overlook some gems and some time look at shine things as better =)


Yes sadly this does seem to be the case. It seems people do like shiny things.

Lucas has done a great job. It just put my computer scientist back up a bit. Im not really a programmer, but more of a computer scientist, so as I say I quite like the theory as well.

To give you an example at UNI at the moment we are having to write a trimmed down version of MySpace using STRUTS, Enterprise Java Beans, J2EE, Javamail, JNDI. All of which is new to me. So did I start to hack out code straight away. NO. What I did do was sit down with a book (some 400 pages long) read it from cover to cover, and then wrote a 100 page design document. Then gave this to my team, who could code quite happily from this document. Albeit with some small mistakes. This took some time. I just think that sometimes people need to stop and think, rather than go, yeah thats amazing, just by how something looks. There is normally more to a bit of code than meets the eye.

You know I personally try quite hard for each of my articles to look at a problem, that people have , or that I have had, and try and solve it. Say threading which most people stuggle with, their is still not a good enough article here about that, so I may look at that after these LINQ ones im currently doing. This article WILL be useful, as I like to go into enough detail that people will get something out of it, that will help them LONG term. Not just a quick GUI Junkie fix, that lasts 5 minutes. For example what about uxTheme.dll support. This has been mentioned in this forum. And is a very valid point, but no one seems to care. Curse them.

But I feel I have now said too much on this.

You seem a nice guy, and know how it is. So ill leave it here.

It was an nice control, that serves a purpose. Thats fine.

Look forward to chatting with you again.

Smile | :) Smile | :) Smile | :) Smile | :) Smile | :)





Smile | :) Smile | :)







sacha barber

GeneralGreat control!! Pin
CSharpian28-Mar-07 20:27
CSharpian28-Mar-07 20:27 
GeneralRe: Great control!! Pin
Lukasz Sw.31-Mar-07 5:43
Lukasz Sw.31-Mar-07 5:43 
GeneralGreat control! Pin
Star Vega28-Mar-07 11:20
Star Vega28-Mar-07 11:20 
GeneralRe: Great control! Pin
Lukasz Sw.31-Mar-07 5:44
Lukasz Sw.31-Mar-07 5:44 
Questionglass buttons Pin
mileswaldron27-Mar-07 11:04
mileswaldron27-Mar-07 11:04 
AnswerRe: glass buttons Pin
Lukasz Sw.27-Mar-07 11:16
Lukasz Sw.27-Mar-07 11:16 
AnswerRe: glass buttons Pin
Lukasz Sw.31-Mar-07 8:08
Lukasz Sw.31-Mar-07 8:08 
GeneralJust to say that's a cool contribution ! Thanks. Pin
2pers26-Mar-07 22:57
2pers26-Mar-07 22:57 
GeneralRe: Just to say that's a cool contribution ! Thanks. Pin
Lukasz Sw.26-Mar-07 23:22
Lukasz Sw.26-Mar-07 23:22 
GeneralThemed buttons Pin
Peter Wone26-Mar-07 14:16
Peter Wone26-Mar-07 14:16 
GeneralRe: Themed buttons Pin
Lukasz Sw.27-Mar-07 11:19
Lukasz Sw.27-Mar-07 11:19 
GeneralRe: Themed buttons Pin
Peter Wone29-Mar-07 0:14
Peter Wone29-Mar-07 0:14 
GeneralRe: Themed buttons Pin
Sacha Barber29-Mar-07 3:59
Sacha Barber29-Mar-07 3:59 
GeneralRe: Themed buttons Pin
Lukasz Sw.31-Mar-07 9:37
Lukasz Sw.31-Mar-07 9:37 
GeneralRe: Themed buttons [modified] Pin
Peter Wone1-Apr-07 13:12
Peter Wone1-Apr-07 13:12 
GeneralRe: Themed buttons Pin
Barbeque Source1-Apr-07 20:28
Barbeque Source1-Apr-07 20:28 
GeneralRe: Themed buttons Pin
Lukasz Sw.2-Apr-07 12:47
Lukasz Sw.2-Apr-07 12:47 

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.