Wednesday, August 10, 2005 4:02 PM Christopher

Insanely simple GradientPanel for WinForms

This has probably been done a hundred times already, but I couldn't find one in the first few pages of google results, so I wrote my own in ~40 lines of C#:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace PostXING.Controls
{
	/// <summary>
	/// GradientPanel is just like a regular panel except it optionally  
	/// shows a gradient.
	/// </summary>
	[ToolboxBitmap(typeof(Panel))]
	public class GradientPanel : Panel
	{
		/// <summary>
		/// Property GradientColor (Color)
		/// </summary>
		private Color _gradientColor;
		public Color GradientColor {
			get { return this._gradientColor;}
			set { this._gradientColor = value;}
		}

		/// <summary>
		/// Property Rotation (float)
		/// </summary>
		private float _rotation;
		public float Rotation {
			get { return this._rotation;}
			set { this._rotation = value;}
		}

		protected override void OnPaint(PaintEventArgs e) {
if(e.ClipRectangle.IsEmpty) return; //why draw if non-visible? using(LinearGradientBrush lgb = new
LinearGradientBrush(this.ClientRectangle,
this.BackColor,
this.GradientColor,
this.Rotation)){ e.Graphics.FillRectangle(lgb, this.ClientRectangle); }

base
.OnPaint (e); //right, want anything handled to be drawn too. } } }

There it is. Enjoy!

[ Currently Playing :  New Way Home - Foo Fighters - The Colour And The Shape (5:42) ]

Filed under: , ,

Comments

# re: Insanely simple GradientPanel for WinForms

Wednesday, August 10, 2005 6:22 PM by mabster

I haven't tried it, but would it be better to call base.OnPaint _after_ the gradient is painted, so that any custom painting performed in the Paint event by the user goes on top of the gradient?

Or have I got that wrong?

mabster

# re: Insanely simple GradientPanel for WinForms

Wednesday, August 10, 2005 8:33 PM by Scott C. Reynolds

Funny. Same thing happened to me about 3 weeks ago. Also funny...Our code is almost identical. Guess there's only so many ways to do a gradient panel. I meant to blog it for exactly the same reason, but forgot.

# re: Insanely simple GradientPanel for WinForms

Thursday, August 11, 2005 3:11 PM by Gabe

Perhaps this is a bit too simple?

I think you should put the painting in OnPaintBackground, otherwise you may get flicker from the default background getting painted immediately before the gradient.

Also, I would check to see if e.ClipRectangle is empty to avoid creating the brush if it's not going to be used.

GNS

# re: Insanely simple GradientPanel for WinForms

Thursday, August 11, 2005 3:24 PM by Chris Frazier

using similar code in the OnPaintBackground event override does nothing.

Good call on the e.ClipRectangle.IsEmpty! Consider it added.

# re: Insanely simple GradientPanel for WinForms

Thursday, August 11, 2005 5:14 PM by Kris

How can you add rounded edges to this panel? I found some samples where you paint the form to provide rounded edges, however when you add controls on it, the controls overflow into the rectangular region. Any thoughts?

Thanks.

# re: Insanely simple GradientPanel for WinForms

Tuesday, August 16, 2005 7:29 PM by Stefan

For Performance it's better to create the Brush not in the Paint-Routine. Create it at the Property-Set!

I mean:

public Color GradientColor {

get { return this._gradientColor;}
set {
this._gradientColor = value;
CreateMyBrush();
}
}

private void CreateMyBrush()
{
creation if rectangle not empty!
}

You need than Dispose() and check if brush was created at OnPaint();

I've done it at my application and the performace at 20 panels on a pentium III 500 was exciting!

# re: Insanely simple GradientPanel for WinForms

Wednesday, August 17, 2005 10:39 AM by Chris Frazier

If I am Disposing() it OnPaint and not creating a new one afterwards, how will the brush be non-null the next time OnPaint is called?

# re: Insanely simple GradientPanel for WinForms

Thursday, July 12, 2007 12:40 AM by test

<a href="test">test</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)