Click here to Skip to main content
Click here to Skip to main content
Go to top

Drop Shadow Label

, 19 Aug 2005
Rate this:
Please Sign up or sign in to vote.
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.

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.

    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.

    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.

    [ 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

Share

About the Author

Michael Ceranski
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 PinmemberDalinor27-Nov-13 9:30 
Questionhow do you put into project Pinmemberseeker5816-Feb-09 5:05 
AnswerRe: how do you put into project PinmemberMichael Ceranski17-Feb-09 2:41 
GeneralASP.NET 2.0 Version Pinmemberrgturner8-Oct-07 2:46 
GeneralHotkey Prefix Support [modified] PinmemberMark Treadwell4-Aug-07 17:52 
GeneralText Align Property Fix PinmemberPrinceCoz8113-Feb-07 9:59 
GeneralBold font error Pinmemberscottk@IntelligentSolutions.com11-Oct-06 5:33 
QuestionChange the border color? Pinmemberwing70prayer6-Oct-06 6:08 
Questioncurve the corners Pinmemberallothernamesaretaken16-Aug-06 20:34 
AnswerRe: curve the corners PinmemberMichael Ceranski18-Aug-06 2:28 
GeneralText Location is locked PinmemberLordRhys6-Sep-05 4:02 
GeneralRe: Text Location is locked PinmemberLordRhys6-Sep-05 5:10 
GeneralTransparent Pinmemberpinturic1-Sep-05 3:52 
GeneralRe: Transparent PinmemberMichael Ceranski1-Sep-05 4:12 
QuestionHow about hot key handle? PinmemberNil Nil21-Aug-05 16:14 
AnswerRe: How about hot key handle? PinmemberMichael Ceranski22-Aug-05 3:23 
GeneralRe: How about hot key handle? PinsussAnonymous31-Aug-05 10:03 
GeneralRe: How about hot key handle? PinmemberMichael Ceranski1-Sep-05 4:13 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 19 Aug 2005
Article Copyright 2005 by Michael Ceranski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid