Click here to Skip to main content
15,885,885 members
Articles / Programming Languages / C#
Article

Drop Shadow Label

Rate me:
Please Sign up or sign in to vote.
4.40/5 (16 votes)
19 Aug 20052 min read 102.9K   2.4K   40   19
Add some flair to your labels.

Demo Application

Introduction

When a customer looks at an application for the first time, they make a lot of decisions based upon the overall appearance of the application. To a developer this can be very frustrating. Many people don’t care if the form that is launched by a button click took ten hours to code. They only care about the appearance. Recently I was getting ready for a software rollout. During my testing and QA I noticed that the plain old Label component bundled in the .NET framework just was too plain to use a banner across the top of my forms. Therefore in order to help the appearance of my form I created a label that can draw a gradient background and some 3D text.

Using the code

I started by creating a new component that derives from the standard System.Windows.Forms.Label control. Then I overrode the Paint method. Originally I hard coded the colors used in the gradient, drop shadow color etc.

C#
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint (e);
    e.Graphics.SmoothingMode = 
      System.Drawing.Drawing2D.SmoothingMode.AntiAlias;          
    
    //draw the gradient background
    LinearGradientBrush brush = new LinearGradientBrush (new 
             Rectangle( 0,0, this.Width, this.Height), 
             Color.White, Color.LightSkyBlue, 0, true );
    e.Graphics.FillRectangle( brush, 0, 0, this.Width, this.Height );
    
    //draw the text - need to draw the shadow first
    e.Graphics.DrawString( this.Text, this.Font, 
       new SolidBrush( Color.Black ), 1, 1, 
       StringFormat.GenericDefault );
    e.Graphics.DrawString( this.Text, this.Font, 
       new SolidBrush( this.ForeColor ), 0, 0, 
       StringFormat.GenericDefault );      
}

After using my control for a while, I decided that I needed to be able to change several properties of my control using the property editor. So I added some private variables and made public properties for them in order for them to appear in the property grid.

C#
private bool _drawGradient = true;
private Color _startColor = Color.White;
private Color _endColor = Color.LightSkyBlue;
private float _angle = 0;

private bool _drawShadow = true;
private float _yOffset = 1;
private float _xOffset = 1;
private Color _shadowColor = Color.Black;

//paint method using new private variables to control appearance
protected override void OnPaint(PaintEventArgs e)
  {
     base.OnPaint (e);
     e.Graphics.SmoothingMode =
        System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

     if( _drawGradient == true )
     {
         LinearGradientBrush brush = new LinearGradientBrush (new
            Rectangle( 0,0, this.Width, this.Height),
            _startColor, _endColor, _angle, true );
         e.Graphics.FillRectangle( brush, 0, 0,
            this.Width, this.Height );
     }

     if( _drawShadow == true )
         e.Graphics.DrawString( this.Text, this.Font,
           new SolidBrush( _shadowColor ), _xOffset,
           _yOffset, StringFormat.GenericDefault );

     e.Graphics.DrawString( this.Text, this.Font,
           new SolidBrush( this.ForeColor ), 0, 0,
           StringFormat.GenericDefault );
  }

If you look at the set methods of the variables used by the Paint method you will notice that whenever I update a value I immediately call the Invalidate() method. If you don’t call this method you will not see your changes in design mode as you modify properties.

C#
public bool DrawGradient
{
    get{ return this._drawGradient; }
    set{ this._drawGradient = value; this.Invalidate(); }
}

In addition to adding design time support by exposing the properties of the component, I also added attributes to the properties. The use of attributes allowed me to specify which category, description and default values (used when the user right clicks in the property grid to reset a value. Modified properties are shown in bold.) would be used when viewing a property in the property grid.

C#
[ Category("Gradient"),
Description("Set to true to draw the gradient background"),
DefaultValue(true)]
public bool DrawGradient
{
    get{ return this._drawGradient; }
    set{ this._drawGradient = value; this.Invalidate(); }
}

I hope someone else has a use for this control. Feel free to add comments and suggestions. The feedback is always welcome.

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


Written By
Software Developer (Senior) Concepts2Code
United States United States
Michael is the co-founder and master consultant for Concepts2Code, a software consulting company based in Buffalo, New York. He's been programming since the early 1990's. His vast programming experience includes VB, Delphi, C#, ASP, ASP.NET, Ruby on Rails, Coldfusion and PHP. Michael also is a Microsoft Certified Application Developer and a Certified Technology Specialist for SQL Server.

Visit his blog.

Comments and Discussions

 
QuestionSpecial characters not showing up right Pin
Dalinor27-Nov-13 9:30
Dalinor27-Nov-13 9:30 
Questionhow do you put into project Pin
seeker5816-Feb-09 5:05
seeker5816-Feb-09 5:05 
AnswerRe: how do you put into project Pin
Michael Ceranski17-Feb-09 2:41
Michael Ceranski17-Feb-09 2:41 
GeneralASP.NET 2.0 Version Pin
rgturner8-Oct-07 2:46
rgturner8-Oct-07 2:46 
GeneralHotkey Prefix Support [modified] Pin
Mark Treadwell4-Aug-07 17:52
professionalMark Treadwell4-Aug-07 17:52 
GeneralText Align Property Fix Pin
PrinceCoz8113-Feb-07 9:59
PrinceCoz8113-Feb-07 9:59 
GeneralRe: Text Align Property Fix Pin
Serg TheMen27-Feb-22 9:31
Serg TheMen27-Feb-22 9:31 
GeneralBold font error Pin
scottk@IntelligentSolutions.com11-Oct-06 5:33
scottk@IntelligentSolutions.com11-Oct-06 5:33 
QuestionChange the border color? Pin
wing70prayer6-Oct-06 6:08
wing70prayer6-Oct-06 6:08 
Questioncurve the corners Pin
allothernamesaretaken16-Aug-06 20:34
allothernamesaretaken16-Aug-06 20:34 
AnswerRe: curve the corners Pin
Michael Ceranski18-Aug-06 2:28
Michael Ceranski18-Aug-06 2:28 
GeneralText Location is locked Pin
LordRhys6-Sep-05 4:02
LordRhys6-Sep-05 4:02 
GeneralRe: Text Location is locked Pin
LordRhys6-Sep-05 5:10
LordRhys6-Sep-05 5:10 
GeneralTransparent Pin
pinturic1-Sep-05 3:52
pinturic1-Sep-05 3:52 
Smile | :) Compliments, I used your control and i found it very nice in many applications of mine.
However i noticed that, if using a transparent background, both the text and the graphics painted in the OnPaint() method are viewed.
Hence I made a little changement to your control so that one can choose to show either the text or the graphics with the aid of a new property.
In case you wish to see what I did please tell me and i will send to you my little update.


GeneralRe: Transparent Pin
Michael Ceranski1-Sep-05 4:12
Michael Ceranski1-Sep-05 4:12 
QuestionHow about hot key handle? Pin
Nil Nil21-Aug-05 16:14
Nil Nil21-Aug-05 16:14 
AnswerRe: How about hot key handle? Pin
Michael Ceranski22-Aug-05 3:23
Michael Ceranski22-Aug-05 3:23 
GeneralRe: How about hot key handle? Pin
Anonymous31-Aug-05 10:03
Anonymous31-Aug-05 10:03 
GeneralRe: How about hot key handle? Pin
Michael Ceranski1-Sep-05 4:13
Michael Ceranski1-Sep-05 4:13 

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

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