Click here to Skip to main content
15,910,981 members
Articles / Multimedia / GDI+
Article

Gradient Forms - The Easy Way

Rate me:
Please Sign up or sign in to vote.
4.30/5 (51 votes)
24 Aug 20054 min read 144.5K   3K   62   41
This article shows you how to easily implement gradients as background in your form.

Image 1

Introduction

I have noticed that a lot of programmers don't bother about the looks of their software. Mostly because they think it is too hard or that it takes too much time. There are certain things that you can do in .NET which can make your application look better with minimal effort. One of those is having a gradient background on your form. Sometimes even a light gradient is enough to make your product stand out in the crowd.

I looked in CodeProject and saw that there was already an article on GradientForms by Erdal Halici. I found his approach rather complicated and lengthy for such an easy task. That's why I wrote this article. This article will show you how to do this in a very easy way, with a small base class that you can use in your application. I promise the only thing you will have to do is to set the colors. It isn't that hard, is it? :)

Laaaadieees and Gentlemeeen, start your Visual Studioooo's!

The good, the bad and the gradient

The good

There are people who take the time to look for the right color for their application. They look for a consistent background that will make their product stand out and recognizable. I applaud this, but why not take it one step further and choose two colors?

The bad

There are people who don't even bother to look for a color. The application just uses that boring gray background color that you see in many applications. You should be ashamed of yourself!

The gradient

And then there are people who apply gradients in their background. It doesn't take any extra effort except for choosing the colors, and your application has that extra visual touch and guess what: Users love these visual things.

Crafting the BaseGradientForm

First things first

The BaseGradientForm is nothing more than a normal form that has some predefined properties and methods. You create it the way you normally create a form. The only thing we do is add some properties and such.

We need three properties to pull it off:

  • Color1: First gradient color.
  • Color2: Second gradient color.
  • ColorAngle: The angle of the gradient.

These do nothing special. These are normal properties like any other property. There's only one difference: when we set a value (from code, or in the design window of our derived form) we need the form to repaint itself. To achieve this we need to call a method that tells our form (or control, when used on a control) to repaint itself. The method used for this purpose is Invalidate. We need the Form to repaint itself when any of these three properties are changed, and thus set. So, we add the line to set all three properties.

C#
private Color _Color1 = Color.Gainsboro;
private Color _Color2 = Color.White;
private float _ColorAngle = 30f;

public Color Color1
{
  get { return _Color1; }
  set 
  { 
    _Color1 = value; 
    this.Invalidate(); // Tell the Form to repaint itself
  }
}

public Color Color2
{
  get { return _Color2; }
  set 
  { 
    _Color2 = value; 
    this.Invalidate(); // Tell the Form to repaint itself
  }
}

public float ColorAngle
{
  get { return _ColorAngle; }
  set 
  { 
    _ColorAngle = value; 
    this.Invalidate(); // Tell the Form to repaint itself
  }
}

Painting our Picasso

Whenever the form gets a call to repaint, either from our Invalidate or from the operating system, it will fire two events: Paint and PaintBackground. It doesn't actually matter in which of the two you put your code, but here we will override PaintBackground to do our thing. In this method, we draw a rectangle onto the form. This rectangle will have the same dimensions as our form. This way it will cover the entire form and will act as the background.

C#
protected override void OnPaintBackground(PaintEventArgs pevent)
{
  // Getting the graphics object
  Graphics g = pevent.Graphics;

  // Creating the rectangle for the gradient
  Rectangle rBackground = new Rectangle(0, 0, 
                            this.Width, this.Height);

  // Creating the lineargradient
  System.Drawing.Drawing2D.LinearGradientBrush bBackground 
      = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground, 
                                        _Color1, _Color2, _ColorAngle);

  // Draw the gradient onto the form
  g.FillRectangle(bBackground, rBackground);

  // Disposing of the resources held by the brush
  bBackground.Dispose();
}

Bugs?

When you test the code that we have written you will notice that strange things happen when you start resizing your form. This is not because critters have taken over your system, it happens because the form doesn't get a 'Repaint Yourself Event' where it resizes. We really have to do everything ourselves, don't we? Luckily, we don't need to do the hard task of overriding the Resize event, but just add one line in the constructor of the BaseFormGradient that tells the form to repaint on resize.

C#
this.SetStyle( ControlStyles.ResizeRedraw, true );

Using the code

BaseForm

You know that when you write an application, and thus create a form you always create a baseform? You do that, right? Just have that baseform inherit from the 'BaseFormGradient', after you add BaseFormGradient.cs to your project, and voila! You have the gradient capability on your form!

No BaseForm?

Don't worry (be happy?), you can still have the gradient capability even if you don't have a baseform. Just have your forms inherit from 'BaseFormGradient', after you add BaseFormGradient.cs to your project.

Applying the gradients

All you need to do is set both the colors and the angle for the gradient. Yes I'm serious, that's all there is to it!

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
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Sure it's easy to create gradients, but... Pin
Uytterhaegen Tommy (Tuy)24-Aug-05 20:23
Uytterhaegen Tommy (Tuy)24-Aug-05 20:23 
GeneralRe: Sure it's easy to create gradients, but... Pin
Miguel Lopes31-Aug-06 5:40
Miguel Lopes31-Aug-06 5:40 
GeneralRe: Sure it's easy to create gradients, but... Pin
PunCha25-Feb-07 22:09
PunCha25-Feb-07 22:09 
QuestionWhy rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
Georgi Atanasov24-Aug-05 7:19
Georgi Atanasov24-Aug-05 7:19 
AnswerRe: Why rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
bigals24-Aug-05 18:56
bigals24-Aug-05 18:56 
GeneralRe: Why rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
Georgi Atanasov25-Aug-05 0:45
Georgi Atanasov25-Aug-05 0:45 
GeneralRe: Why rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
Jeff Firestone25-Jan-06 5:37
Jeff Firestone25-Jan-06 5:37 
AnswerRe: Why rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
Uytterhaegen Tommy (Tuy)24-Aug-05 20:33
Uytterhaegen Tommy (Tuy)24-Aug-05 20:33 
First of all, it might seem very obvious for you and for anyone who has been toying with GDI+, but I have been on many projected and have noticed that everyone seem to think it is to hard, they want it, but it is way to hard. Takes te much time, etc.

That's the reason I have put up this article. And yes I know that the performance would be better if you cache te brush, but be real, will you notice the difference ? The code, like it is now, is a lot easier to understand for people who have little or no knowledge of GDI+. The code gets harder for the more functionality you put in it.

This is an article to get people interested in looks, they can go on from there.

And secondly, this is my first article, I won't invest tons of time in an article before I know that it will be read, see this as a first try and test for me (my knowledge of GDI+ & C# goes way beyond making a simple gradient, as you put it). So to my feeling this was the right choice for a topic. It's not to hard, can make your programs look a lot better, and I can see what kind of feedback is given here to decide if I will create other, more in debt, articles.

You probably have some, or a lot of knowledge of GDI+ and programming, a lot of people don't, and even if they do. It can be outside .NET. The team I'm in at the moment consist of all experienced programmers, but they still are somewhat surprised if they see what things I do with GDI+ in the short ammount of time.

So I can imagine that people that don't know of GDI+ can do a lot with this article, and will score it like that to.



GeneralHehe... Pin
Georgi Atanasov25-Aug-05 0:38
Georgi Atanasov25-Aug-05 0:38 
GeneralRe: Hehe... Pin
Uytterhaegen Tommy (Tuy)25-Aug-05 0:54
Uytterhaegen Tommy (Tuy)25-Aug-05 0:54 
GeneralRe: Hehe... Pin
CWinThread25-Aug-05 2:47
CWinThread25-Aug-05 2:47 
GeneralRe: Hehe... Pin
Georgi Atanasov25-Aug-05 3:56
Georgi Atanasov25-Aug-05 3:56 
GeneralRe: Hehe... Pin
Uytterhaegen Tommy (Tuy)25-Aug-05 4:05
Uytterhaegen Tommy (Tuy)25-Aug-05 4:05 
GeneralRe: Hehe... Pin
c2j229-Aug-05 20:40
c2j229-Aug-05 20:40 
GeneralRe: Why rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
Dave Cross9-Aug-10 2:27
professionalDave Cross9-Aug-10 2:27 
AnswerRe: Why rating 4.09??? For showing how to create a LinearGradientBrush??? Pin
Black Dhalia7-Mar-07 23:21
Black Dhalia7-Mar-07 23:21 
QuestionDefining where gradient starts? Pin
Mirad24-Aug-05 2:27
Mirad24-Aug-05 2:27 
AnswerRe: Defining where gradient starts? Pin
Uytterhaegen Tommy (Tuy)24-Aug-05 2:49
Uytterhaegen Tommy (Tuy)24-Aug-05 2:49 

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.