5,276,156 members and growing! (19,494 online)
Email Password   helpLost your password?
Desktop Development » Button Controls » General     Intermediate License: The Common Public License Version 1.0 (CPL)

Creating a glass button using GDI+

By Lukasz Swiatkowski

How to create an animating glass button using only GDI+ (and not using WPF).
C# 2.0, C#, Windows, .NET, .NET 2.0, WinForms, VS2005, VS, Design, Dev

Posted: 19 Feb 2007
Updated: 21 Nov 2007
Views: 136,042
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
Prize winner in Competition "Best C# article of Jan 2007"
137 votes for this Article.
Popularity: 10.07 Rating: 4.71 out of 5
2 votes, 1.5%
1
2 votes, 1.5%
2
1 vote, 0.7%
3
17 votes, 12.5%
4
114 votes, 83.8%
5

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 (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)

About the Author

Lukasz Swiatkowski


Mvp
I'm studying computer science at Wroclaw University of Technology, Poland.

My interests: .NET, reading, programming, drawing, OBE and LD, Japan, yoga, tai-chi.
My favourite movie: "Star Trek: First Contact".

My website: www.lukesw.net
My email: lukasz.swiatkowski/*at-sign*/gmail.com.
Location: Poland Poland

Other popular Button Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 195 (Total in Forum: 195) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralVB Versionmemberraing314:42 25 Jun '08  
GeneralRe: VB VersionmvpLukasz Swiatkowski0:47 26 Jun '08  
GeneralAwesome Button!memberEngr4869:22 7 Jun '08  
GeneralRe: Awesome Button!mvpLukasz Swiatkowski0:37 26 Jun '08  
QuestionLicencememberJulian-w4:03 17 May '08  
AnswerRe: LicencemvpLukasz Swiatkowski7:10 17 May '08  
GeneralRe: LicencememberJulian-w8:18 17 May '08  
GeneralRe: LicencememberJulian-w3:50 18 May '08  
GeneralRe: LicencemvpLukasz Swiatkowski3:59 18 May '08  
GeneralDrawToBitmapmemberJacob Shepherd21:41 20 Apr '08  
GeneralRe: DrawToBitmapmvpLukasz Swiatkowski6:33 21 Apr '08  
GeneralRe: DrawToBitmapmemberJacob Shepherd15:11 28 Apr '08  
GeneralRe: DrawToBitmapmvpLukasz Swiatkowski8:42 29 Apr '08  
QuestionUse in unmanaged Appmemberneoger10:38 30 Mar '08  
GeneralRe: Use in unmanaged AppmvpLukasz Swiatkowski8:42 31 Mar '08  
GeneralWeb Appmemberbulljustin11:10 24 Mar '08  
GeneralRe: Web AppmvpLukasz Swiatkowski7:08 28 Mar '08  
GeneralBug or Feature ?memberStulle11:30 7 Mar '08  
GeneralRe: Bug or Feature ?mvpLukasz Swiatkowski8:28 9 Mar '08  
GeneralRe: Bug or Feature ?memberStulle11:06 13 Mar '08  
GeneralRe: Bug or Feature ?memberDamir122:30 16 Mar '08  
GeneralRe: Bug or Feature ?mvpLukasz Swiatkowski6:55 17 Mar '08  
GeneralRe: Bug or Feature ?mvpLukasz Swiatkowski7:19 17 Mar '08  
GeneralLicensememberDamir13:10 27 Feb '08  
GeneralRe: LicensemvpLukasz Swiatkowski8:21 9 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Nov 2007
Editor: Sean Ewington
Copyright 2007 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project