Introduction

You can learn a lot about .NET Windows Forms programming by building a custom control. There are several books on the topic, but you'll soon find yourself reaching for Google to answer questions about Forms, GDI+, and Visual Studio you don't even know how to ask. When you find answers, they will be frustratingly incomplete.

What better way to learn?

That's how it went for me when I wrote Aqua Button. Since this was a learning project and I wasn't bound by practicality, I set out to build a button that looks and feels like push buttons in Apple Mac OS X. Apple's user interface is called Aqua®, and it's alive with transparent, colorful controls. Aqua buttons and Windows buttons have some things in common, but they also have several rather large differences:

Aqua buttons pulse when they are the default button

Aqua buttons are not in the tab order

Aqua buttons generally do not have keyboard equivalents

Aqua buttons do not press down when clicked -- they change color instead

So, it's safe to say that AquaButton won't satisfy Windows interface guidelines. But it may help you make that leap from using Windows Forms controls to designing and building your own custom controls.

Drawing the 3D button

AquaButton has a 3D look with text shadows, button shadows, and highlights. While it may be possible to recreate this look with GDI+ in OnPaint, I took the easier path and created the button bitmaps in Photoshop. I used PixelJerk's Photoshop action to create my initial source bitmap, then removed the background layer and merged the remaining layers to make the button partially transparent. I sliced that bitmap into three segments: a left end cap (left.png), a right end cap (right.png), and a single-pixel column from the middle (fill.png). Each time AquaButton paints itself, it uses DrawImage to quickly draw the two end caps, and FillRectangle to fill in the body. This means that you can set the width of AquaButton, but not the height.

If you need taller or thinner buttons, replace the source bitmaps with your own, then set the ButtonHeight class constant to the height of your bitmap. If your bitmaps have a shadow, set the ButtonShadowOffset class constant so that it specifies the distance from the bottom of the button to the bottom of the image. AquaButton uses this last constant to center the label on the button.

Aqua buttons are aqua-colored when they are the default button (specified with the Form.AcceptButton property). Non-default buttons draw in grayscale. I didn't need to manage separate source bitmaps just to draw the button in grayscale -- it's easy enough to draw the button in grayscale using GDI+ ImageAttributes. AquaButton declares ImageAttribute and ColorMatrix variables for each state:

I setup the image attributes and color matrices in InitializeGraphics. I use the cmDefault color matrix to make the button lighter (you'll see why in a minute, when I explain how I use gamma correction to simulate the pulse effect):

That's all there is to drawing AquaButton in it's basic states. With just a little more code, we can extend this to make AquaButton pulse.

Making the button pulse

Aqua buttons pulse with a glow that seems to originate inside the button. I considered using a GIF-like animation with a sequence of bitmaps showing the button in several intermediate states of illumination, controlled by a timer. While this would allow me to create realistic lighting in Photoshop, I would need many intermediate bitmaps to create a fluid animation.

I decided instead to use Gamma Correction, a simpler technique that sacrifices some lighting quality. Earlier I showed you how I lightened up the default and normal button images using a ColorMatrix. I did this so that I can use gamma correction to draw lighter (1.8 gamma) and darker (0.7 gamma) versions of the image using gamma correction. Change PulseGammaMin and PulseGammaMax if these look too light or dark.

This is how it works. AquaButton starts a timer to invalidate itself every 70 milliseconds (PulseInterval). On each timer tick, AquaButton uses gamma correction to draw itself progressively lighter or darker, with almost seamless transitions. My first attempt looked more like blinking than pulsing -- the button bounced almost immediately from light to dark. So I added logic to slow the lighting change as it approaches min or max gamma. If you're not happy with the way it looks, tune the PulseGammaShift, PulseGammaReductionThreshold, and PulseGammaShiftReduction constants. Here is the gamma shift logic from TimeOnTick:

Supporting Visual Design

AquaButton exposes several properties to support the Visual Studio designer:

Pulse - determines whether an AquaButton pulses when it is the default button.

SizeToLabel - determines whether AquaButton automatically sets it's width based on it's label. Set this to true, then set the button label. The AquaButton will automatically size itself at design time.

AquaButton also shadows several properties from System.Windows.Forms.Control:

Size - AquaButtons have a fixed height, so it doesn't make sense to allow you to set Size (which includes Height) in the Visual Studio property grid. For lack of a better solution, I hid this property from the Visual Studio property grid using a custom designer (see below).

Height - AquaButtons don't reveal their Size property, so you need another way to see Height. I shadowed Control.Height and made it browsable and read only in the property grid.

Width - As with Height, I shadowed this property and made it browsable in the property grid. You decide whether to set width explicitly, or use SizeToLabel to automatically size the button.

I also wrote a custom designer, Wildgrape.Aqua.Controls.ButtonDesigner, to filter out properties that don't make sense for AquaButton: AllowDrop, BackColor, BackgroundImage, ContextMenu, FlatStyle, ForeColor, Image, ImageAlign, ImageIndex, ImageList, Size, and TextAlign. I did this to simplify visual design, but I did not bother to shadow them to prevent callers from setting them in code.

Extending AquaButton

I've already mentioned a few ways to customize AquaButton. If you're looking for a learning project, here are a few ideas.

AquaButton looks like an Aqua button, but behaves differently when it comes to selection. You could extend AquaButton to implement these missing behaviors to make AquaButton more faithful to the Aqua look and feel:

Aqua buttons are not in the tab order, but AquaButton leaves that decision to you.

Aqua buttons do not receive focus, so the default button is always the default button, and pulses even when another control has focus. AquaButton inherits .NET button selection behavior, which means you can make another button the default button simply by tabbing or mousing to it.

Or you could go the other way and make AquaButton behave more like .NET Windows Forms buttons:

Add focus hints

Make the selected button pulse (even if it isn't the default button)

Allow users to set the button's height (one reader has suggested a solution -- see the feedback for this article)

I'm interested to see how you extend AquaButton. I would be happy to post your enhancements here and give you credit.

Credits

AquaButton is an independent creation and has not been authorized, sponsored, or otherwise approved by Apple Computer, Inc. Aqua is a trademark of Apple Computer, Inc.

Revisions

September 12, 2002 - Readers pointed out that the button wasn't forwarding Click events to the form. The problem was that I was doing too much in the mouse tracking code, and not giving the base class a chance to process events. After experimenting with Button events, I rewrote the mouse tracking logic and made it much simpler.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

I downloaded the source, converted it with the wizard to Visual Studio 2010 solution without errors. I recompiled both the Control and the Demo app. Both build without any hitch.

However, when I try to display the demo form the designer fails saying

"The type 'Wildgrape.Aqua.Controls.Button' has no property named 'Size'

this is very strange because it does and it doesn't give a compilation error.

However, if I go to the Button.Designer.cs class and remove the line that removes the "Size" Property in the Post-something, then everything seems to be fine. That of course is just a work around, don't have time to fool around.

Another issue seems to be that the Wildgrape button control never shows up on the Toolbox and can't be added either.

Natura Cleanse stands out as the most effective colon cleanser today. The product is 100% safe as it comprises all natural ingredients that have high antioxidant capability. This specific ability of the product makes it one of the kind in staying cleansed from the insides and vibrant.

I think refer to this MAC-UI suite, we can findout additional improvement ideas for this button. For example: Any Color for the button and text, button Styles, Different button colors and text colors for 4 states,better alignment and allocation for text and image...
MAC-UI suite contains also 30 controls for building MAC-UI style application on .NET. (http://www.econtechvn.com/en/macuisuite_detail.htm)

If I set SizeToLabel to false and specify the Width (125 in this case) in my design view the width of the control is not set when I run the program. Instead the width looks as if it defaults to ButtonDefaultWidth = 80.
This seems to be because when I alter the width in the design view no corresponding statement is put into the "Windows Form Designer generated code" section of my code. I would expect "this.AddAgentAquaButton.Width = 125;" to appear in my code. If I put this line in manually it works fine, but something is suppressing the generation of this code in the designer.
On further investigation this seems tp be caused because the Size property is suppressed in ButtonDesigner. If the line Properties.Remove( "Size" ); is commented out then the Size, and the Width, can be set.
I'd be interested to know if anyone else has had this problem, and what they did about it.

I am new to C#. I used the Wildgrape.Aqua.Controls.dll file but I don't know how to modify it so the click event doesn't fire twice. I really would like to know how to have that done. By The way the button Rocks

How can I make the Button transparent to a background image?
With the original code and with your change, the button set the transparency to the background color of the form but not to the background image... Any ideia?

function will copy the background color from upper left corner of the form. If the form is transparent in this corner the background color of the button will be different from the form one. So I use this code instead:

perhaps I found some good things that should be improved to make it perfect
-the duplicated events
-the transparency...(yes , because If you want to put the botton on a dark background it becames really ungly ( and unreadable );

Hi, there is no doubt that the button looks great. However, if the button is created on top of a panel, and the panel's backcolor is set to transparent, the button's color looks really bad. Anyone knows how to solve this problem?

Your fix seems to work for small buttons. However if the height is set to above about 40 the rounded edges do not make a smooth join onto the top and bottom edges. ie they interset at different angles.

I noticed that the demo sample came with a DLL called "Wildgrape.Aqua.Controls.dll" which I extracted into C:\Windows\System32. However, when I try to run the demo application under Windows XP, it just crashes, giving the message:- "Application has generated an exception that could not be handled. Process id=0xbd0 [3024] Thread id-0xbe4 [3044]". This happens every time, even after re-booting.

Do I maybe need some other DLL's that only ship with .Net? I'm not a .NET user. I've only got VC++ 6 installed at the moment.