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

Creating a Glass Button using GDI+

, 26 Mar 2013 CPL
Rate this:
Please Sign up or sign in to vote.
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:

<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:

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.
    • Splitted 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)

Share

About the Author

Lukasz Swiatkowski
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Technology, Poland.
 
My interests: reading, programming, drawing, Japan, yoga, tai-chi.
 
My websites: luminous.codeplex.com, www.lukesw.net

Comments and Discussions

 
GeneralRe: Awesome Button! PinmvpLukasz Swiatkowski26-Jun-08 0:37 
QuestionLicence PinmemberJulian-w17-May-08 4:03 
AnswerRe: Licence PinmvpLukasz Swiatkowski17-May-08 7:10 
GeneralRe: Licence PinmemberJulian-w17-May-08 8:18 
GeneralRe: Licence PinmemberJulian-w18-May-08 3:50 
GeneralRe: Licence PinmvpLukasz Swiatkowski18-May-08 3:59 
GeneralDrawToBitmap PinmemberJacob Shepherd20-Apr-08 21:41 
GeneralRe: DrawToBitmap PinmvpLukasz Swiatkowski21-Apr-08 6:33 
GeneralRe: DrawToBitmap PinmemberJacob Shepherd28-Apr-08 15:11 
GeneralRe: DrawToBitmap PinmvpLukasz Swiatkowski29-Apr-08 8:42 
QuestionUse in unmanaged App Pinmemberneoger30-Mar-08 10:38 
GeneralRe: Use in unmanaged App PinmvpLukasz Swiatkowski31-Mar-08 8:42 
GeneralWeb App Pinmemberbulljustin24-Mar-08 11:10 
GeneralRe: Web App PinmvpLukasz Swiatkowski28-Mar-08 7:08 
No. WindowsForms controls cannot be used on a web page.
 
Lukasz
 
~~~~~~~~~~~~~~~~~~~
My website: www.lukesw.net

QuestionBug or Feature ? PinmemberStulle7-Mar-08 11:30 
AnswerRe: Bug or Feature ? PinmvpLukasz Swiatkowski9-Mar-08 8:28 
GeneralRe: Bug or Feature ? PinmemberStulle13-Mar-08 11:06 
GeneralRe: Bug or Feature ? PinmemberDamir116-Mar-08 22:30 
GeneralRe: Bug or Feature ? PinmvpLukasz Swiatkowski17-Mar-08 6:55 
GeneralRe: Bug or Feature ? PinmvpLukasz Swiatkowski17-Mar-08 7:19 
GeneralLicense PinmemberDamir127-Feb-08 3:10 
GeneralRe: License PinmvpLukasz Swiatkowski9-Mar-08 8:21 
GeneralRe: License PinmemberDamir19-Mar-08 8:31 
GeneralRe: License PinmvpLukasz Swiatkowski9-Mar-08 8:34 
GeneralThumbs up for this article! Pinmembermikepc23-Feb-08 14:10 
GeneralRe: Thumbs up for this article! PinmvpLukasz Swiatkowski23-Feb-08 14:17 
Questiontransparency color Pinmembertcsoccerman26-Nov-07 13:06 
AnswerRe: transparency color PinmemberLukasz Swiatkowski29-Nov-07 8:28 
QuestionSuggestion Pinmemberdamianrda19-Nov-07 6:04 
AnswerRe: Suggestion PinmemberLukasz Swiatkowski19-Nov-07 7:39 
Generalnice job Pinmembercrystalbug14-Nov-07 16:30 
GeneralRe: nice job PinmemberLukasz Swiatkowski15-Nov-07 1:08 
GeneralLove the control PinmemberFoxman20006-Nov-07 20:45 
GeneralRe: Love the control PinmemberLukasz Swiatkowski15-Nov-07 1:01 
QuestionHow About Circular Edges PinmemberSukhjinder_K7-Oct-07 21:37 
AnswerRe: How About Circular Edges PinmemberLukasz Swiatkowski13-Oct-07 5:15 
GeneralRe: How About Circular Edges PinmemberSukhjinder_K13-Oct-07 7:34 
QuestionLicencing? PinmemberJonas Follesø3-Oct-07 23:55 
AnswerRe: Licencing? PinmemberLukasz Swiatkowski4-Oct-07 2:02 
GeneralRe: Licencing? Pinmemberivanorcan26-Oct-07 5:52 
GeneralRe: Licencing? PinmemberLukasz Swiatkowski28-Oct-07 14:02 
Generalnice work dude Pinmemberteeboss4-Sep-07 6:35 
GeneralRe: nice work dude PinmemberLukasz Swiatkowski4-Sep-07 6:44 
GeneralThank you Pinmemberparasaniasandip16-Aug-07 19:47 
GeneralRe: Thank you PinmemberLukasz Swiatkowski17-Aug-07 4:24 
GeneralChange color when disabled PinmemberTavbi13-Aug-07 22:42 
GeneralRe: Change color when disabled PinmemberLukasz Swiatkowski16-Aug-07 11:56 
GeneralRe: Change color when disabled PinmemberTavbi16-Aug-07 21:16 
GeneralImprove Glow Speed PinmemberSukhjinder_K2-Aug-07 22:34 
GeneralRe: Improve Glow Speed PinmemberLukasz Swiatkowski3-Aug-07 8:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 27 Mar 2013
Article Copyright 2007 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid