Click here to Skip to main content
Licence 
First Posted 4 Jan 2004
Views 173,365
Bookmarked 63 times

Color Button for the .NET Compact Framework

By | 4 Jan 2004 | Article
Shows how to write a color button control for the .NET compact framework.

Sample Image - ColourButtons.gif

Introduction

I have written this article for two reasons. The first is that, while the standard .NET framework allows you to change the color of buttons, this feature is missing from the compact framework. The buttons in the compact framework are a boring black on grey. The second reason for writing this article is that, whilst it is relatively straightforward to write standard controls, writing controls for the compact framework is slightly more challenging, since there isn't a project template for the compact framework controls in Visual Studio. I would imagine that both of these issues will be resolved in time by Microsoft, but for the time being, a user control is required and this control is not trivial to write.

Creating the Control

There are already several articles describing how to create a control for the compact framework, so I won't duplicate those articles here. In my opinion, one of the better articles is on the MSDN site: Creating Custom Controls for the .NET Compact Framework by Chris Kinsman, Vergent Software, September 2002. It took me a while to get this to work, but that turned out to be because there are quite a few steps to follow and you have to make sure you don't miss any out!

The Code

Having created a blank control, I set about changing it to work as a button. I wanted to create a button that behaved the same way as the standard .NET CF button, but had more colors. I decided to give the button, 4 new color properties. These are:

Color m_NormalBtnColour = Color.LightYellow;
Color m_NormalTxtColour = Color.Blue;
Color m_PushedBtnColour = Color.Blue;
Color m_PushedTxtColour = Color.Yellow;

The properties of these are exposed by the control using the following code (which shows one of the four - the others are identical with different names and descriptions):

#if NETCFDESIGNTIME
    [Category("Appearance")]
    [DefaultValue(3)]
    [Description("The normal colour of the button.")]
#endif
public Color NormalBtnColour
{
    set
    {
        m_NormalBtnColour = value;
        Invalidate();
    }
    get
    {
        return m_NormalBtnColour;
    }
}

Note that I invalidate the control after the color has been changed. This is so that the control is redrawn when the color is changed whilst designing the form. I also had to remove the two standard color properties: BackColor and ForeColor. I did this with the following code (one of two shown):

#if NETCFDESIGNTIME
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
#endif
public override Color BackColor
{
    set
    {;}
    get
    { return new Color(); }
}

Browsable(false) removes the item from the property window, and EditorBrowsable(Never) prevents intellisense from displaying the property. Finally, for the attributes, I added the button state, which is either normal or pushed.

public enum States 
{ 
    Normal, 
    Pushed 
}

States m_state;

This was set to normal in the constructor, and would be set to pushed on a mouse down event and back to normal on a mouse up event. Also, in order that the button was correctly drawn in design mode, OnResize was overridden to invalidate the control when the button was resized.

protected override void OnMouseDown(
            System.Windows.Forms.MouseEventArgs e) 
{
    m_state = States.Pushed; 
    // button receives input focus
    Focus();  
    base.OnMouseDown(e); 
    Invalidate(); 
} 

protected override void OnMouseUp(
            System.Windows.Forms.MouseEventArgs e) 
{
    m_state = States.Normal;
    base.OnMouseUp(e);
    Invalidate();
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    Invalidate();
}

The only thing left to do was the OnPaint method. This draws the button using the two normal colors if the button is in the normal state, or the two pushed colors if the button is in the pushed state.

protected override void OnPaint(PaintEventArgs e) 
{
    Graphics graphics = e.Graphics;

    Pen pen;
    SolidBrush brush;
    SolidBrush textBrush;

    //Work out the colours that we should be using
    // for the text and background
    if (m_state == States.Normal)
    {
        brush = new SolidBrush(m_NormalBtnColour);
        textBrush = new SolidBrush(m_NormalTxtColour);
        pen = new Pen(m_NormalTxtColour);
    }
    else
    {
        brush = new SolidBrush(m_PushedBtnColour);
        textBrush = new SolidBrush(m_PushedTxtColour);
        pen = new Pen(m_PushedTxtColour);
    }

    //Draw a rectangle and fill the inside
    graphics.FillRectangle(brush, 0, 0, Width, Height);
    graphics.DrawRectangle(pen, 0, 0, Width-1, Height-1);

    //Create a font based on the default font
    int fontHeight = 10;
    Font font = new Font(FontFamily.GenericSerif,
               fontHeight, FontStyle.Bold);

    //Find out the size of the text
    SizeF textSize = new SizeF();
    textSize = e.Graphics.MeasureString(Text, font);

    //Work out how to position the text centrally
    float x=0,y=0;
    if (textSize.Width < Width)
        x = (Width - textSize.Width) /2;
    if (textSize.Height < Height)
        y = (Height - textSize.Height) /2;

    //Draw the text in the centre of the button using
    // the default font
    graphics.DrawString(Text, font, textBrush, x, y);
}

As described in the "building controls" articles, there are 2 solutions, one for the actual control, and one for the control in the designer. I wrote a batch file to build both solutions and copy the assemblies to the correct location, so that they could be picked up by Visual Studio. Once the controls have been added to "My User Controls", they can be dragged onto forms and manipulated just like any other control.

History

  • 05 Jan 2004 - Initial version.

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

Jon Nethercott

Software Developer (Senior)
CodeWrite Ltd.
United Kingdom United Kingdom

Member

Jon is a Software engineer with over 25 years of experience, the last 9 of which have been using C# and ASP.NET. Previously he has used C++ and MFC. He has a degree in Electronic Systems Engineering.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalchange background color when disabled Pinmemberkarmo1289021:13 22 Sep '10  
QuestionWhere do you set the default size of the button? Pinmemberlagu265320:32 27 Sep '06  
GeneralCORAL 64 / CORAL66 Pinmemberblashyrk12323:06 29 Mar '06  
Hello Jon,
 
my name is Aaron Jones and I am hailing from Edith Cowan University in Perth, Western Australia.
I am emailing in regards to CORAL64/CORAL66. I know this is probably off-topic for the association of the board, but I am desperately seeking information (anything and everything) about the CORAL language. There seems to be references online, but very little meaningful informationConfused | :confused: , apart from xgc.com . Is CORAL still classified? I saw a letter from the British Defence secretary officialy stating CORAL was no longer being used in the Royal Navy.
 
As a summary of the types of information, I guess what I am looking for is:
 
syntax and semantics, lexical analysis and parsing, names, bindings, scope, data types and records, expressions and assignments, conditional and unconditional program flow. I seem to uncomfortably understand that it is possible to both interpret and compile the language.
As for definitions of actual COMPILER for coral64 / coral66, I would be interested too. It makes sense that CORAL is classified being onboard a military vessel, as well as there is little reference to it Smile | :)
 
It would be great if you could at least point me in the right direction for resources on CORAL. CORAL code may help to a certain degree, such that I can ascertain the above mentioned classifications. if you are interested in where I got your email address, I can give you the source.
 
I thankyou for your council,
Aaron Jones
GeneralFonts not changing PinmemberJay Dubal3:59 31 May '05  
GeneralRe: Fonts not changing PinmemberJay Dubal4:02 31 May '05  
GeneralMisc. Comments PinmemberWhizzard999210:19 21 Oct '04  
GeneralTrubles with adding control to the form PinmemberAndrew Khimenko1:05 14 Jul '04  
GeneralRe: Trubles with adding control to the form PinmemberJon Nethercott9:08 14 Jul '04  
GeneralIncrease draw speed PinmemberRojM17:13 8 Jun '04  
GeneralRe: Increase draw speed PinmemberJon Nethercott12:56 9 Jun '04  
General3D style buttons PinmemberGlamiac7:32 23 Jan '04  
GeneralRe: 3D style buttons PinmemberJon Nethercott11:52 25 Jan '04  
GeneralRe: 3D style buttons PinmemberCJCraft.com13:30 18 Feb '04  
GeneralColor Button is disabled in the toolbox PinsussGourou8:29 14 Jan '04  
GeneralRe: Color Button is disabled in the toolbox PinmemberJon_UK395:54 18 Jan '04  
Generalnice article PinmemberJermo7:52 8 Jan '04  
QuestionNon trivial control? PinmemberCarlos H. Perez5:10 6 Jan '04  
AnswerRe: Non trivial control? PinmemberJon_UK397:18 6 Jan '04  
GeneralEmulator Skin PinmemberFurty13:59 5 Jan '04  
GeneralRe: Emulator Skin PinmemberJon_UK3921:13 5 Jan '04  
GeneralRe: Emulator Skin PinmemberFurty21:39 5 Jan '04  
GeneralRe: Emulator Skin PinmemberJon_UK3923:03 5 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 5 Jan 2004
Article Copyright 2004 by Jon Nethercott
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid