Click here to Skip to main content
15,867,308 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 919.8K   21K   610   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: License Pin
Lukasz Sw.9-Mar-08 7:21
Lukasz Sw.9-Mar-08 7:21 
GeneralRe: License Pin
Damir19-Mar-08 7:31
Damir19-Mar-08 7:31 
GeneralRe: License Pin
Lukasz Sw.9-Mar-08 7:34
Lukasz Sw.9-Mar-08 7:34 
GeneralThumbs up for this article! Pin
mikepc23-Feb-08 13:10
mikepc23-Feb-08 13:10 
GeneralRe: Thumbs up for this article! Pin
Lukasz Sw.23-Feb-08 13:17
Lukasz Sw.23-Feb-08 13:17 
Questiontransparency color Pin
tcsoccerman26-Nov-07 12:06
tcsoccerman26-Nov-07 12:06 
AnswerRe: transparency color Pin
Lukasz Sw.29-Nov-07 7:28
Lukasz Sw.29-Nov-07 7:28 
QuestionSuggestion Pin
damianrda19-Nov-07 5:04
damianrda19-Nov-07 5:04 
AnswerRe: Suggestion Pin
Lukasz Sw.19-Nov-07 6:39
Lukasz Sw.19-Nov-07 6:39 
Generalnice job Pin
crystalbug14-Nov-07 15:30
crystalbug14-Nov-07 15:30 
GeneralRe: nice job Pin
Lukasz Sw.15-Nov-07 0:08
Lukasz Sw.15-Nov-07 0:08 
GeneralLove the control Pin
Foxman20006-Nov-07 19:45
Foxman20006-Nov-07 19:45 
GeneralRe: Love the control Pin
Lukasz Sw.15-Nov-07 0:01
Lukasz Sw.15-Nov-07 0:01 
QuestionHow About Circular Edges Pin
Sukhjinder_K7-Oct-07 20:37
Sukhjinder_K7-Oct-07 20:37 
AnswerRe: How About Circular Edges Pin
Lukasz Sw.13-Oct-07 4:15
Lukasz Sw.13-Oct-07 4:15 
GeneralRe: How About Circular Edges Pin
Sukhjinder_K13-Oct-07 6:34
Sukhjinder_K13-Oct-07 6:34 
QuestionLicencing? Pin
Jonas Follesø3-Oct-07 22:55
Jonas Follesø3-Oct-07 22:55 
AnswerRe: Licencing? Pin
Lukasz Sw.4-Oct-07 1:02
Lukasz Sw.4-Oct-07 1:02 
GeneralRe: Licencing? Pin
ivanorcan26-Oct-07 4:52
ivanorcan26-Oct-07 4:52 
GeneralRe: Licencing? Pin
Lukasz Sw.28-Oct-07 13:02
Lukasz Sw.28-Oct-07 13:02 
Generalnice work dude Pin
TuanNH854-Sep-07 5:35
TuanNH854-Sep-07 5:35 
GeneralRe: nice work dude Pin
Lukasz Sw.4-Sep-07 5:44
Lukasz Sw.4-Sep-07 5:44 
GeneralThank you Pin
parasaniasandip16-Aug-07 18:47
parasaniasandip16-Aug-07 18:47 
GeneralRe: Thank you Pin
Lukasz Sw.17-Aug-07 3:24
Lukasz Sw.17-Aug-07 3:24 
GeneralChange color when disabled Pin
Tavbi13-Aug-07 21:42
Tavbi13-Aug-07 21:42 

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.