Click here to Skip to main content
15,881,033 members
Articles / Multimedia / GDI+
Article

Gradient Panel Custom Control

Rate me:
Please Sign up or sign in to vote.
3.73/5 (13 votes)
7 Dec 20063 min read 95.8K   4K   44   13
This article describes a quick and simple approach to creating a gradient panel custom control.

Introduction

This article describes a quick and simple approach to creating a gradient panel custom control. In this example, the standard Windows Forms Panel container control is extended to provide a persistent gradient background; this differs from using an approach where a gradient background is created dynamically against the graphics context of the Panel. Even though the background is persistent, it is dynamically updated whenever it is resized. Figure 1 illustrates the control in use:

Image 1

Figure 1. Gradient Panel Control in Use

Getting Started

In order to get started, start up the Visual Studio 2005 IDE and open the included project. The solution consists of a WinForms project with a single form and a single custom control included. The form is used as a test bed for the custom control, and the custom control is an extension of the standard Windows Forms Panel control. Figure 2 shows the Solution Explorer for this project:

Image 2

Figure 2. Solution Explorer

The Code: Main Form

The main form of the application does not contain any meaningful code; the form itself contains two instances of the gradient panel control, and the gradient panel control is configured through the property editor for the purposes of the control test.

As the main form is trivial, I will not describe it any further in this document.

The Code: Gradient Panel Control

The GradientPanel control is a custom control built to extend the standard Windows Forms Panel control; the only additions made to the standard control are those required to render the gradient background at run time.

If you open the code up and examine the imports, you will note the following imports prior to the namespace and class declaration.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace GradientPanel
{
    public partial class GradientPanel : System.Windows.Forms.Panel  
    {

After the class declaration, the code creates two member variables used to hold the beginning and ending colors of the gradient. After the declaration of these two variables, the class constructor calls the PaintGradient method, which in turn paints the gradient on the panel:

C#
// member variables
System.Drawing.Color mStartColor;
System.Drawing.Color mEndColor;

public GradientPanel()
{
    InitializeComponent();
    PaintGradient();
}

protected override void OnPaint(PaintEventArgs pe)
{
    // TODO: Add custom paint code here

    // Calling the base class OnPaint
    base.OnPaint(pe);
}

The PaintGradient method called in the class constructor is described next:

C#
private void PaintGradient()
{
            
     System.Drawing.Drawing2D.LinearGradientBrush gradBrush;
     gradBrush = 
       new System.Drawing.Drawing2D.LinearGradientBrush(new 
       Point(0, 0), new Point(this.Width, this.Height), 
       PageStartColor, PageEndColor);

    Bitmap bmp = new Bitmap(this.Width, this.Height);

    Graphics g = Graphics.FromImage(bmp);
    g.FillRectangle(gradBrush, new Rectangle(0, 0, 
                    this.Width, this.Height));
    this.BackgroundImage = bmp;
    this.BackgroundImageLayout = ImageLayout.Stretch;
}

The PaintGradient method is used to generate and size the linear gradient brush; this method creates a bitmap and applies the gradient to the bitmap. The background image property is then set to point at the bitmap, which results in the display gradient behind the panel. Using this approach, the controls placed in the container do not interfere with the display of the gradient.

At design time, the control user may set these properties in the property grid, or the values may be set in the code.

Image 3

Figure 3. Properties for Start and End Colors

Summary

This article described an approach to making a persistent gradient background on a Panel control based custom control. The control provided in the example could be dropped into other projects and used as is. The purpose of the control is to provide a tool for making a gradient panel without drawing the gradient directly on the graphics context of the panel page.

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

Comments and Discussions

 
QuestionNeed license details - Would like to use this code in my application Pin
James Gopinath9-Jul-14 19:36
James Gopinath9-Jul-14 19:36 
QuestionDid me great favors Pin
jokefan20-May-14 15:55
jokefan20-May-14 15:55 
GeneralNice one!!! Pin
Prabhu_Pappu11-Oct-13 0:43
Prabhu_Pappu11-Oct-13 0:43 
GeneralMy Vote of 5 Pin
trantrum21-Jun-13 3:30
professionaltrantrum21-Jun-13 3:30 
GeneralMy vote of 1 Pin
winheart28-Dec-10 18:20
winheart28-Dec-10 18:20 
QuestionScrolling? Pin
Marty Garton7-Jun-10 7:42
Marty Garton7-Jun-10 7:42 
When I use this panel and set auto scroll to true, it freaks out when you scroll. The gradient messes up and the backgrounds for the items in the panel are no longer drawn properly. Any suggestions?
GeneralGradient panel Pin
khaja093417-Jul-08 3:03
khaja093417-Jul-08 3:03 
GeneralAnother cool panel in .net Pin
Ramesh Soni18-Jun-07 8:27
Ramesh Soni18-Jun-07 8:27 
GeneralRe: Page not found - Did you mean...? Pin
fuschi1231-Apr-10 8:19
fuschi1231-Apr-10 8:19 
QuestionWhy? Pin
Ken Hadden12-Dec-06 8:34
Ken Hadden12-Dec-06 8:34 
GeneralWhy not use OnPaintBackGround [modified] Pin
mardoek11-Dec-06 23:49
mardoek11-Dec-06 23:49 
GeneralRe: Why not use OnPaintBackGround Pin
Ranjan.D27-Sep-07 20:51
professionalRanjan.D27-Sep-07 20:51 
GeneralRe: Why not use OnPaintBackGround Pin
rotu.codeproject7-Oct-08 20:50
rotu.codeproject7-Oct-08 20:50 

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.