5,666,979 members and growing! (15,584 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Beginner

An Aqua Button Control Written in C# for .Net

By Rad1cal

I have written a number of applications and always been a little disappointed at how drab they look using the standard VS controls. I had been looking for an easy way for brightening up these apps and in the end resorted to writing my own button control.
C# 1.0, C# 2.0, C#Windows, .NET, .NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, TabletPCVS.NET2003, VS2005, Visual Studio, Dev

Posted: 10 Oct 2006
Updated: 18 Oct 2006
Views: 35,829
Bookmarked: 64 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 5.44 Rating: 4.33 out of 5
0 votes, 0.0%
1
1 vote, 5.6%
2
1 vote, 5.6%
3
6 votes, 33.3%
4
10 votes, 55.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - Sample Image.jpg

Introduction

I have written a number of applications and always been a little disappointed at how drab they look using the standard VS controls. I had been looking for an easy way for brightening up these apps and in the end resorted to writing my own button control based on the "The Aqualizer Strikes Back!" work found on the Code Project site. 

Using this, control bitmaps are created "on the fly" according to the attributes of the control. Colour, Height, Width, Font Size and Text. The slow rendering speed of the graphics is negated by using cached bitmaps. For each button two Png files are produced. One is the colour of the button and the other which is clear and is used when the mouse hovers over the button and when the button is disabled. Using this control you gain the advantages of both options - flexible image generation and - speed.

Using the Control in your own Applications

The control can be used as a normal button control "as is". Simply add the project to your solution and you can add the control to your forms. Remember the aquabutton control must be compiled before it can be added to the form in the form editor. Use the properties ButtonText, ButtonColour, FontSize and TextColour to set the text and colours of the button.

Points of Interest

New Properties on the Button

Using VS 2003 I spent quite some time trying to make Text and ForeColor do my bidding. For one reason or another I found these to be unreliable and decided in the end to use my own properties. Hence ButtonText, ButtonColour, FontSize and TextColour are used.

Making the Button's Text Property viewable on the form editor

To add my own properties to the Aqua Button Control I found it useful to use the Description, Default Value and the Browsable attributes. The sample below shows how this is. Useful tips in setting the properties can be given in the description field.

[Description("The text visible on the control. Use \\n for line breaks")]
[DefaultValue("Not Set")
[Browsable()>
public string ButtonText
{
    set
    {
        base.Text="not set";
        initialise();
        m_text=value;
        redrawButton();
    }

    get
    {
        return m_text;
    }
}

Deriving your own Button Classes

If in a moment of madness, if you feel inclined to base your own control on the Aqua Button control, it is perfectly possible to override the CreateImage() method to produce alternative designs (e.g. diamonds, emeralds etc). The helper classes for the graphics are are provided for doing all of this. You may have to make some of them public.
To understand how to do this, just use the class provided to get yourself started. For more information about how the algorithms work I suggest you take a look at the excellent "Aqualiser Strikes Back" article. In overriding  CreateImage() be sure to cache the images in the same way as the base class or performance will suffer.
private void createBaseImages()
{
    /* Call the overrideable CreateImage routines to get the bitmaps/images */
    m_image=CreateImage( m_buttonColour );
    m_nofocusImage=CreateImage( Color.White );
    this.Image = m_image;
}

Limitations

  • The behaviour of the button is slightly limited in that it does not provide the dialog type behaviour. With minor tweaks (or with me being prompted) this may be remedied in future releases.
  • The buttons still lack feedback on pressing. They need either a click are a visual action to complete the effect.
  • The 2D graphics library used dictates that the code is compiled with the "allow unsafe code" option set. For this reason it is best to hive the control into it's own library as not to enforce this option on other assemblies.
  • The bitmap is not rendered until the the Text property is changed from "Not Set". At this point every time the font, size, text or colour is changed a new bitmap is rendered. This is OK for released applications, but can mean a lot of bitmaps can be created in your %temp% directory during development.

Feedback

I use this control a lot in my own applications as it brightens up the UI dramatically. I realise that the control is not a completely finished and has shortcomings (not serious ones hopefully). Feedback is gratefully accepted and where fitting will be incorporated in later versions of the control.

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.

A list of licenses authors might use can be found here

About the Author

Rad1cal



Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Miscellaneous 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 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralHow to change shape of the button?membersarvind19073:26 5 Aug '08  
GeneralCan the text be made brighter?memberDutchJohn10:55 11 May '08  
GeneralRe: Can the text be made brighter?memberaberg10:47 6 Jul '08  
GeneralCannot Download or view ScreenshotsmemberSukhjinder_K21:00 8 Dec '07  
GeneralHow to asign short cut keymembersudhir paliwal5:10 22 Oct '07  
GeneralRe: How to asign short cut keymemberRad1cal5:11 29 Nov '07  
GeneralCan Aqua Buttons be used with Web Form??memberB-RadG11:10 18 Jul '07  
GeneralRe: Can Aqua Buttons be used with Web Form??memberRad1cal7:44 24 Jul '07  
Generalvery nicememberAlexandros2005100:47 20 Oct '06  
GeneralRe: very nicememberRad1cal13:40 20 Oct '06  
Questionvery coolmemberwurakeem3:41 17 Oct '06  
AnswerRe: very coolmemberRad1cal6:17 17 Oct '06  
AnswerRe: very coolmemberRad1cal1:10 18 Oct '06  
GeneralVery nice visualmemberLutz Morrien0:36 17 Oct '06  
GeneralWhich Visual Studio was that?...memberPetru665:26 17 Oct '06  
GeneralRe: Which Visual Studio was that?...memberRad1cal6:15 17 Oct '06  
GeneralRe: Which Visual Studio was that?...memberPetru666:23 17 Oct '06  
GeneralRe: Which Visual Studio was that?...memberRad1cal1:11 18 Oct '06  
GeneralRe: Which Visual Studio was that?...memberRad1cal1:07 18 Oct '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 18 Oct 2006
Editor:
Copyright 2006 by Rad1cal
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project