![]() |
Desktop Development »
Button Controls »
General
Intermediate
License: The Common Public License Version 1.0 (CPL)
Creating a Glass Button using GDI+By Lukasz SwiatkowskiHow to create an animating glass button using only GDI+ (and not using WPF) |
C# 2.0, C# 3.0, VB 8.0, VB 9.0.NET 2.0, Win2K, WinXP, Win2003, Vista, TabletPC, .NET 3.0, .NET 3.5, WinForms, VS2008, Dev, Design
|
|
Advanced Search |
|
|
||||||||||||||||||
Sample application using a standard glass button with image.

The same application, but this time it has a customized glass button.

MFC application which hosts four glass buttons.

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.
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.
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Nov 2008 Editor: Sean Ewington |
Copyright 2007 by Lukasz Swiatkowski Everything else Copyright © CodeProject, 1999-2009 Web12 | Advertise on the Code Project |