Click here to Skip to main content
Licence 
First Posted 19 Aug 2005
Views 58,733
Bookmarked 36 times

Drop Shadow Label

By | 19 Aug 2005 | Article
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

About the Author

Michael Ceranski

Software Developer (Senior)
Concepts2Code
United States United States

Member

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.

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
Questionhow do you put into project Pinmemberseeker585:05 16 Feb '09  
AnswerRe: how do you put into project PinmemberMichael Ceranski2:41 17 Feb '09  
GeneralASP.NET 2.0 Version Pinmemberrgturner2:46 8 Oct '07  
GeneralHotkey Prefix Support [modified] PinmemberMark Treadwell17:52 4 Aug '07  
GeneralText Align Property Fix PinmemberPrinceCoz819:59 13 Feb '07  
GeneralBold font error Pinmemberscottk@IntelligentSolutions.com5:33 11 Oct '06  
QuestionChange the border color? Pinmemberwing70prayer6:08 6 Oct '06  
Questioncurve the corners Pinmemberallothernamesaretaken20:34 16 Aug '06  
AnswerRe: curve the corners PinmemberMichael Ceranski2:28 18 Aug '06  
GeneralText Location is locked PinmemberLordRhys4:02 6 Sep '05  
GeneralRe: Text Location is locked PinmemberLordRhys5:10 6 Sep '05  
GeneralTransparent Pinmemberpinturic3:52 1 Sep '05  
GeneralRe: Transparent PinmemberMichael Ceranski4:12 1 Sep '05  
QuestionHow about hot key handle? PinmemberNil Nil16:14 21 Aug '05  
AnswerRe: How about hot key handle? PinmemberMichael Ceranski3:23 22 Aug '05  
GeneralRe: How about hot key handle? PinsussAnonymous10:03 31 Aug '05  
GeneralRe: How about hot key handle? PinmemberMichael Ceranski4:13 1 Sep '05  

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
Web01 | 2.5.120517.1 | Last Updated 19 Aug 2005
Article Copyright 2005 by Michael Ceranski
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid