Click here to Skip to main content
15,887,290 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 924.3K   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
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 
GeneralRe: Themed buttons Pin
Patrick Etc.1-Apr-07 16:43
Patrick Etc.1-Apr-07 16:43 
Peter Wone wrote:
At any rate I guess that what I'm saying is that I can't see the point of custom rendering of standard UI elements inside an application - it's unnecessary and it prevents the application from dynamically assuming the color scheme, window ornament glyphs and so on of the host platform.

Well, one - who are you to say it's unnecessary? There's a reason that designers and developers have been at odds for so long - this is one of them. Designers want to create a spiffy looking application, developers are limited a) by the tools and b) by the fact that the implementation and the design are inextricably linked.

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. 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?! you may be asking. This is why Microsoft publishes design guidelines for custom controls. The point is NOT what the controls look like - the point is how they behave. It's an interesting facet of UI development that the visual appearance of an interface has little to do with its discoverability - that is, how much a user is going to comprehend how to use it. As long as the functionality is common across applications, that's all that matters. Of course, certain elements need to be common, like the dropdown area on a ComboBox or DropDownButton, but the rest of the button can appear however you want it to and the users will still be able to understand what it does.

In that sense, it's a tragedy that so many applications look so blasted cookie cutter. That also happens to be why I love controls like this one. They change nothing about the fundamental behavior of the application, but they can say alot about the care and attention to detail that has been put into the design.

Finally, I don't know about you but I absolutely hate the Windows themes that hack uxtheme.dll. One, they NEVER have all of the visual elements - something is always left out, even if you don't run into until 2 weeks later, and since I do so much development and "power-user" types of activities on my machine, I tend to notice those faults almost immediately. Also, Windows obviously complains when uxtheme.dll gets hacked, which I don't enjoy having to put up with. I've never found a themer (not even Windows Blinds) that I could tell was completely stable, and that's because these themers are fundamentally *HACKS* of the operating system. Not good.
GeneralRe: Themed buttons Pin
Peter Wone2-Apr-07 15:02
Peter Wone2-Apr-07 15:02 
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 

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.