65.9K
CodeProject is changing. Read more.
Home

xpLabel - XP Enabled Label Control

Feb 10, 2007

LGPL3

2 min read

viewsIcon

44610

downloadIcon

1179

XP-enabled label control to change the look and feel of Windows application

Test application

Introduction

The xpLabel is a simple label control with an extension that it has different look-and-feel from traditional label control. It uses color schemes to make itself look-like some of the aesthetic label controls available otherwise. However, you can also always customize the color scheme and your creativity is the limit to it.

Using the code

Lets start analyzing the code. The first thing to consider is the types used.

/// <summary>
/// Type for handling the BackColorScheme
/// </summary>
public enum BackColorSchemeType
{
    /// <summary>
    /// Office 2003 Blue Color
    /// </summary>
    Office2003Blue,
    /// <summary>
    /// Office 2003 Orange
    /// </summary>
    Office2003Orange,
    /// <summary>
    /// Office 2003 Silver
    /// </summary>
    Office2003Silver,
    /// <summary>
    /// Office 2003 Green
    /// </summary>
    Office2003Green,
    /// <summary>
    /// Dark Blue Tinge
    /// </summary>
    DarkBlueTinge,
    /// <summary>
    /// OSX Light Gray
    /// </summary>
    OSXLightGray,
    /// <summary>
    /// Lime Green
    /// </summary>
    LightGreenVikings,
    /// <summary>
    /// Azure color
    /// </summary>
    Azure
}

/// <summary>
/// Type for handling the text-alignment on the control
/// </summary>
public enum TextAlignStyle
{
    /// <summary>
    /// Aligns Left-top
    /// </summary>
    TAlignLeftTop,
    /// <summary>
    /// Aligns Left-middle
    /// </summary>
    TAlignLeftMiddle,
    /// <summary>
    /// Aligns Left-bottom
    /// </summary>
    TAlignLeftBottom,
    /// <summary>
    /// Aligns Middle-top
    /// </summary>
    TAlignMiddleTop,
    /// <summary>
    /// Aligns Middle-middle
    /// </summary>
    TAlignMiddleMiddle,
    /// <summary>
    /// Aligns Middle-bottom
    /// </summary>
    TAlignMiddleBottom
}

Extra properties that are exposed to client are

/// <summary>
/// Starting color of gradient.
/// </summary>
[Description("Starting color of Gradient"),
Category("Gradient Colors")]
public Color BackColor1
{
    get{ return _bkgcolo1; }
    set
    { 
        if( (_bkgcolo1 != value) )
        {
            _bkgcolo1 = value; 
            Invalidate();
        }
    }
}

/// <summary>
/// Ending color of gradient.
/// </summary>
[Description("Ending color of Gradient"),
Category("Gradient Colors")]
public Color BackColor2
{
    get{ return _bkgcolo2; }
    set
    { 
        if( (_bkgcolo2 != value) )
        {
            _bkgcolo2 = value; 
            Invalidate();
        }
    }
}

/// <summary>
/// Drop shadow for the text.
/// </summary>
[Description("Show Shadow for the text")]
public bool ShowShadow
{
    get{ return _shadow; }
    set
    {
        _shadow = value;
        Invalidate();
    }
}

/// <summary>
/// BackColorScheme: To use pre-defined color scheme.
/// </summary>
[Description("Color scheme to be used")]
public BackColorSchemeType BackColorScheme
{
    get{ return _bkgColorScheme; }
    set
    {
        if( _bkgColorScheme != value )
        {
            _bkgColorScheme = value;
            ChangeColorScheme();
            Invalidate();
        }
    }
}

/// <summary>
/// The way to align text on the control
/// </summary>
[Description("Sets the alignment of the text")]
public TextAlignStyle TextAlign
{
    get{ return _taStyle; }
    set
    {
        if( _taStyle != value )
        {
            _taStyle = value;
            Invalidate();
        }
    }
}

The BackColor1 and BackColor2 are used to make the background 3D view. The ShowShadow makes the text drop a shadow on the background. The BackColorScheme is used to select one of the pre-defined color scheme and TextAlign is used to align the text on the control.

The control implementation uses two helper functions as

private void ChangeColorScheme()
{
    switch(BackColorScheme)
    {
        case BackColorSchemeType.Office2003Blue:
            BackColor = Color.FromArgb(159, 191, 236);
            BackColor1 = Color.FromArgb(159, 191, 236);
            BackColor2 = Color.FromArgb(54, 102, 187);
            break;
        case BackColorSchemeType.Office2003Orange:
            BackColor = Color.FromArgb(251, 230, 148);
            BackColor1 = Color.FromArgb(251, 230, 148);
            BackColor2 = Color.FromArgb(239, 150, 21);
            break;
        case BackColorSchemeType.DarkBlueTinge:
            BackColor = Color.FromArgb(89, 135, 214);
            BackColor1 = Color.FromArgb(89, 135, 214);
            BackColor2 = Color.FromArgb(4, 57, 148);
            break;
        case BackColorSchemeType.OSXLightGray:
            BackColor = Color.FromArgb(242, 242, 242);
            BackColor1 = Color.FromArgb(242, 242, 242);
            BackColor2 = Color.FromArgb(200, 200, 200);
            break;
        case BackColorSchemeType.LightGreenVikings:
            BackColor = Color.FromArgb(235, 245, 214);
            BackColor1 = Color.FromArgb(235, 245, 214);
            BackColor2 = Color.FromArgb(195, 224, 133);
            break;
        case BackColorSchemeType.Office2003Silver:
            BackColor = Color.FromArgb(225, 226, 236);
            BackColor1 = Color.FromArgb(225, 226, 236);
            BackColor2 = Color.FromArgb(150, 148, 178);
            break;
        case BackColorSchemeType.Office2003Green:
            BackColor = Color.FromArgb(234, 240, 207);
            BackColor1 = Color.FromArgb(234, 240, 207);
            BackColor2 = Color.FromArgb(178, 193, 140);
            break;
        case BackColorSchemeType.Azure:
            BackColor = Color.FromArgb(222, 218, 202);
            BackColor1 = Color.FromArgb(222, 218, 202);
            BackColor2 = Color.FromArgb(192, 185, 154);
            break;
    }
}

private PointF CalculateTextLocation()
{
    float x = 0.0f, y = 0.0f;

    switch(TextAlign)
    {
        case TextAlignStyle.TAlignLeftTop:
            x = 2;
            y = 0;
            break;
        case TextAlignStyle.TAlignLeftBottom:
            x = 2;
            y = Height - 15;
            break;
        case TextAlignStyle.TAlignLeftMiddle:
            x = 2;
            y = (Height - 15) / 2;
            break;
        case TextAlignStyle.TAlignMiddleTop:
            x = (Width - 80) / 2;
            y = 0;
            break;
        case TextAlignStyle.TAlignMiddleMiddle:
            x = (Width - 80) / 2;
            y = (Height - 15) / 2;
            break;
        case TextAlignStyle.TAlignMiddleBottom:
            x = (Width - 80) / 2;
            y = (Height - 15);
            break;
        default:
            throw new InvalidEnumArgumentException("
                    Invalid Argument for Text Alignment");
    }
    return new PointF(x, y);
}

The ChangeColorScheme() defined above just changes the BackColor properties according to the scheme-type selected by the client and CalculateTextLocation() finds the (x,y) location where the text is to be drawn.

Now comes the real drawing part in the OnPaint() method which contains the following code

base.OnPaint (e);

// Sets the smoothing-mode to AntiAlias
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

// The gradient brush that uses BackColor1 and BackColor2 with 
// a horizontal gradient tinge
LinearGradientBrush lgb = new LinearGradientBrush(
    new Rectangle(0, 0, Width, Height), BackColor1, BackColor2,
    90, false);
e.Graphics.FillRectangle(lgb, 0, 0, Width, Height);
lgb.Dispose();

// The following code is used to draw label text
SolidBrush tb = new SolidBrush(ForeColor);
e.Graphics.MeasureString(Text, Font, Width);
e.Graphics.DrawString(Text,Font,tb,CalculateTextLocation());
tb.Dispose();

// The shadow is dropped with an offset of .5f
if( ShowShadow )
{
    SolidBrush tb1 = new SolidBrush(ForeColor);
    e.Graphics.MeasureString(Text, Font, Width);
    e.Graphics.DrawString(Text, Font, tb1, 
        CalculateTextLocation().X + 0.5f, 
        CalculateTextLocation().Y + 0.5f);
    tb1.Dispose();
}

Before, we close the discussion about the code, let's see what are other positions where the control is Invalidated (i.e. re-painted).

public xpLabel()
{
    // This call is required by the Windows.Forms Form 
    // Designer.
    InitializeComponent();

    // my code
    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    this.SetStyle(ControlStyles.DoubleBuffer, true);
    this.SetStyle(ControlStyles.ResizeRedraw, true);
    Text = this.Name;
    BackColor = Color.FromArgb(159, 191, 236);
    ChangeColorScheme();
}

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

For more updated download, you can visit my website [http://amitbhandari.co.nr].

Known Issues

Since I'm very new to control/component writing so this control may contain some bugs. So, any comments, criticisms, bugs, issues...are all welcome.

One known issue which I have found with the control is that whenever you change ColorScheme in the properties window, the BackColor, BackColor1 and BackColor2 doesn't get updated immediately. I'm working on it and hope to resolve it very soon!!!

History

v1.0.2594.34043 First initial release.