Click here to Skip to main content
13,632,114 members
Click here to Skip to main content
Add your own
alternative version

Stats

788.2K views
18.9K downloads
605 bookmarked
Posted 19 Feb 2007
Licenced CPL

Creating a Glass Button using GDI+

, 26 Mar 2013
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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: VS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
Lukasz Swiatkowski21-Feb-07 23:07
memberLukasz Swiatkowski21-Feb-07 23:07 
GeneralRe: VS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
KN112322-Feb-07 13:24
memberKN112322-Feb-07 13:24 
GeneralRe: VS2005 can't recognize Glass.DoubleBufferedPanel(); Pin
gurumosch22-Feb-07 23:14
membergurumosch22-Feb-07 23:14 
GeneralCool stuff Pin
Nishant Sivakumar21-Feb-07 15:34
mvpNishant Sivakumar21-Feb-07 15:34 
GeneralRe: Cool stuff Pin
Lukasz Swiatkowski21-Feb-07 23:09
memberLukasz Swiatkowski21-Feb-07 23:09 
GeneralAwesome! Pin
markvd21-Feb-07 15:12
membermarkvd21-Feb-07 15:12 
GeneralRe: Awesome! Pin
Lukasz Swiatkowski21-Feb-07 23:01
memberLukasz Swiatkowski21-Feb-07 23:01 
QuestionVista? Pin
Jasmine250121-Feb-07 5:37
memberJasmine250121-Feb-07 5:37 
AnswerRe: Vista? Pin
Lukasz Swiatkowski21-Feb-07 6:59
memberLukasz Swiatkowski21-Feb-07 6:59 
QuestionRe: Vista? Pin
joe.hanna21-Feb-07 10:36
memberjoe.hanna21-Feb-07 10:36 
AnswerRe: Vista? Pin
Lukasz Swiatkowski21-Feb-07 11:19
memberLukasz Swiatkowski21-Feb-07 11:19 
GeneralRe: Vista? Pin
AlexEvans22-Feb-07 9:00
memberAlexEvans22-Feb-07 9:00 
GeneralRe: Vista? Pin
Lukasz Swiatkowski22-Feb-07 9:52
memberLukasz Swiatkowski22-Feb-07 9:52 
GeneralRe: Vista? Pin
AlexEvans22-Feb-07 10:08
memberAlexEvans22-Feb-07 10:08 
GeneralRe: Vista? Pin
Lukasz Swiatkowski22-Feb-07 10:19
memberLukasz Swiatkowski22-Feb-07 10:19 
GeneralRe: Vista? Pin
AlexEvans22-Feb-07 10:33
memberAlexEvans22-Feb-07 10:33 
GeneralRe: Vista? Pin
Lukasz Swiatkowski22-Feb-07 10:51
memberLukasz Swiatkowski22-Feb-07 10:51 
AnswerRe: Vista? Pin
eisernWolf21-Feb-07 9:40
membereisernWolf21-Feb-07 9:40 
GeneralExcellent Pin
Pang Wu20-Feb-07 21:51
memberPang Wu20-Feb-07 21:51 
GeneralRe: Excellent Pin
Lukasz Swiatkowski21-Feb-07 7:01
memberLukasz Swiatkowski21-Feb-07 7:01 
GeneralAbsolutly one of the best Button-Control I've seen here... Pin
tim_mcgwyn20-Feb-07 9:45
membertim_mcgwyn20-Feb-07 9:45 
GeneralRe: Absolutly one of the best Button-Control I've seen here... Pin
Patrick Sears20-Feb-07 11:13
memberPatrick Sears20-Feb-07 11:13 
NewsRe: Absolutly one of the best Button-Control I've seen here... Pin
Lukasz Swiatkowski21-Feb-07 10:23
memberLukasz Swiatkowski21-Feb-07 10:23 
GeneralRe: Absolutly one of the best Button-Control I've seen here... Pin
Patrick Sears21-Feb-07 10:54
memberPatrick Sears21-Feb-07 10:54 
GeneralRe: Absolutly one of the best Button-Control I've seen here... [modified] Pin
Patrick Sears21-Feb-07 15:26
memberPatrick Sears21-Feb-07 15:26 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180712.1 | Last Updated 27 Mar 2013
Article Copyright 2007 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid