Click here to Skip to main content
15,886,362 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 923.9K   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: 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 
GeneralRe: Themed buttons Pin
Patrick Etc.1-Apr-07 16:43
Patrick Etc.1-Apr-07 16:43 
GeneralRe: Themed buttons Pin
Peter Wone2-Apr-07 15:02
Peter Wone2-Apr-07 15:02 
In essence we are debating style vs substance. Substance without style is useful. Style without substance is not. Ergo, substance is more important than style.
In fact, if what you were saying were really valid, Microsoft wouldn't have gone the direction they did with WPF in .NET 3.0 - where the ENTIRE visual appearance of controls is not in the SLIGHTEST related to the functionality of the control.
I think you will find that Microsoft does not care whether you conform or not so long as you bind to Windows and buy Microsoft tools. Microsoft is well aware that some people will go their own way no matter how expensive or ill-advised, and that a standardised mechanism like WPF will mitigate the problems these people create. It's like letting your children smoke dope at home with friends because it's better than them smoking dope on the street with strangers. In this metaphor, Microsoft is also selling the dope. So Microsoft has addressed your point that the implementation and the design have historically been inextricably entangled. This incidentally reduces costs for products like Office, which are so functionally mature that the scope for new versions (and new revenue) are limited to style changes. This doesn't mean that style is more important than substance, it means that for MS the substance is defined and their concerns have refocused on style. I have discussed this with a chum of mine who is a product manager at MS and he confirms my interpretation (although he is not PM for Office so it's still a matter of opinion).
The whole concept of 'standard controls' has been thrown out the window in Vista and in WPF, and this is because Microsoft does not WANT people to use standard controls. The out-of-the-box button in WPF is NOT styled by Windows themes - it is the plain pre-Windows XP flat gray button. Any theming on the button has to be done by the application designer.
What Microsoft wants is a high quality low maintenance product that sells well. To this end, separation of the presentation mechanism for standard controls has been progressively more unified, pervasive and platform-supported with every iteration of the platform (Windows). The primary motivation for this is to allow a unified look and feel consistent with all the other applications running on the same platform. Periodic appearance of unthemed buttons merely reflects the existence of code that predates an effective platform-wide theming mechanism.

Cookie-cutter is an objective, because UI elements form a language. In any given epoch there has been a lingua franca. Those who fail to embrace the lingua franca of their time are excluded from commerce and generally fade into history. This applies in both commercial and cultural contexts.

The problems you have encountered with theming tools have more to do with the fact that most of them interfere with the shell rather than simply changing the resources used by the standard shell. The Vista theme currently installed on my system is entirely implemented as an mstheme file and supporting resources and requires a hacked uxtheme.dll merely because it isn't signed. I have found this to have no more impact on my system than switching from the default XP theme to the Olive or Silver themes. This is a development system and subject to the same instabilities of configuration and use that you describe.

I reverted to the standard blue theme for the lingua franca reasons cited above; it is inappropriate to use Vista style screen snaps in documentation for users who typically use XP. Eating one's own philosophical dogfood, y'see.

PeterW
--------------------
If you can spell and use correct grammar for your compiler, what makes you think I will tolerate less?

GeneralRe: Themed buttons Pin
Sacha Barber29-Mar-07 3:57
Sacha Barber29-Mar-07 3:57 
QuestionCan this be used as a webcontrol? Pin
Perdue26-Mar-07 9:29
Perdue26-Mar-07 9:29 
AnswerRe: Can this be used as a webcontrol? Pin
Lukasz Sw.26-Mar-07 9:50
Lukasz Sw.26-Mar-07 9:50 
GeneralDemo and Dll split Pin
Marcos Meli23-Mar-07 14:03
Marcos Meli23-Mar-07 14:03 
GeneralRe: Demo and Dll split Pin
Lukasz Sw.24-Mar-07 6:29
Lukasz Sw.24-Mar-07 6:29 
GeneralRe: Demo and Dll split Pin
Marcos Meli24-Mar-07 7:40
Marcos Meli24-Mar-07 7:40 
GeneralRe: Demo and Dll split Pin
Lukasz Sw.26-Mar-07 23:43
Lukasz Sw.26-Mar-07 23:43 
GeneralRe: Demo and Dll split Pin
Marcos Meli28-Mar-07 6:25
Marcos Meli28-Mar-07 6:25 
GeneralRe: Demo and Dll split Pin
Lukasz Sw.31-Mar-07 11:44
Lukasz Sw.31-Mar-07 11:44 
GeneralIm a bit dubious about this Pin
Sacha Barber20-Mar-07 0:56
Sacha Barber20-Mar-07 0:56 
GeneralRe: Im a bit dubious about this Pin
vanad.min20-Mar-07 9:49
vanad.min20-Mar-07 9:49 
GeneralRe: Im a bit dubious about this Pin
Sacha Barber20-Mar-07 23:39
Sacha Barber20-Mar-07 23:39 
GeneralRe: Im a bit dubious about this Pin
vanad.min21-Mar-07 13:20
vanad.min21-Mar-07 13:20 
GeneralRe: Im a bit dubious about this Pin
Sacha Barber22-Mar-07 0:53
Sacha Barber22-Mar-07 0:53 
GeneralRe: Im a bit dubious about this Pin
Marcos Meli23-Mar-07 12:57
Marcos Meli23-Mar-07 12:57 
GeneralRe: Im a bit dubious about this Pin
BlaiseBraye28-Mar-07 20:00
BlaiseBraye28-Mar-07 20:00 
GeneralRe: Im a bit dubious about this Pin
Sacha Barber28-Mar-07 23:51
Sacha Barber28-Mar-07 23:51 

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.