65.9K
CodeProject is changing. Read more.
Home

Gradient Label Control For WinForms

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.47/5 (17 votes)

May 11, 2003

1 min read

viewsIcon

107911

downloadIcon

1832

Create background gradient color bar

Sample Image - GradientLabel.jpg

Introduction

As all of us know GDI+ which is supported by .NET has many new features for graphics. One of them is the gradient brush. Gradient brushes can also be used to draw lines, curves, and paths. You can use a linear gradient brush to fill a shape with color that changes gradually as you move across the shape. For example, suppose you create a horizontal gradient brush by specifying blue at the left edge of a shape and green at the right edge. When you fill that shape with the horizontal gradient brush, the color changes gradually from blue to green as you move from the shape's left edge to its right edge.

I think it is very useful and so I have written a control called GLabel that inherits from Label control of System.Windows.Forms. This GLabel has backgound with gradient color. Because it is a linear gradient it needs two colors; Left color and Right color for linear gradient. When you use it on a WinForm, you should set two colors via properties BeginColor and EndColor of GLabel class.

Code listing

Following is this GLabel class:

public class GLabel : System.Windows.Forms.Label
{
 // declare two color for linear gradient
  private Color cLeft;
  private Color cRight;

   // property of begin color in linear gradient
  public Color BeginColor
  {
    get
    {
      return cLeft;
    }
    set
    {
      cLeft = value;
    }
  }
   // property of end color in linear gradient
  public Color EndColor
  {
    get
    {
      return cRight;
    }
    set
    {
      cRight = value;
    }
  }
  public GLabel()
  {
     // Default get system color 
    cLeft = SystemColors.ActiveCaption;
    cRight = SystemColors.Control;
  }
  protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  {
     // declare linear gradient brush for fill background of label
    LinearGradientBrush GBrush = new LinearGradientBrush(
        new Point(0, 0),
        new Point(this.Width, 0), cLeft, cRight);
    Rectangle rect = new Rectangle(0,0,this.Width,this.Height);
     // Fill with gradient 
    e.Graphics.FillRectangle(GBrush, rect);

     // draw text on label
    SolidBrush drawBrush = new SolidBrush(this.ForeColor);
    StringFormat sf = new StringFormat();
     // align with center
    sf.Alignment = StringAlignment.Center;
     // set rectangle bound text
    RectangleF rectF = new 
    RectangleF(0,this.Height/2-Font.Height/2,this.Width,this.Height);
     // output string
    e.Graphics.DrawString(this.Text, this.Font, drawBrush, rectF, sf);
  }
} 

This class above is simple. You can develop it to serve your ideas. You can rewrite OnPaint as well, such as write more alignment styles for text. This code is written fixed for the top-center in rectangle (this rectangle contains rectangle of GLabel at center). When you put GLabel control in a WinForm you can write code as shown below:

public class Form1 : System.Windows.Forms.Form
{
  private GLabel gLabel1;
  private System.ComponentModel.Container components = null;  
 
  public Form1()
  {
    //Required for Windows Form Designer support
    InitializeComponent();
    //TODO:
    this.gLabel1 = new GLabel();
    this.gLabel1.BeginColor = System.Drawing.SystemColors.ActiveCaption;
    this.gLabel1.EndColor = System.Drawing.SystemColors.Control;
    this.gLabel1.Name = "gLabel1";
    this.gLabel1.Font = new Font("Verdana", 12F, FontStyle.Bold );
    this.gLabel1.Size = new System.Drawing.Size(350, 50);
    this.gLabel1.TabIndex = 0;
    this.gLabel1.Text = "Nguyen Ha Giang - Vietnam - HCMC";
    this.Controls.Add(gLabel1);
  }
  .......
}

Conclusion

That's all, thanks a lot for reading from Nguyen Ha Giang, Vietnam, HCM City.