Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Creating the Background

Last updated Dec 9, 2004.

The background is constructed using a PathGradientBrush that fills the entire client area of the window. Constructing it is a little bit convoluted. You have to create a GraphicsPath object, define the points that make up the polygon that you want to use for your brush, add those lines to the GraphicsPath object that you created, and then construct a PathGradientBrush from that and tell it what colors you want in the center and at each point. GDI+ handles blending the colors from the center out to the corners, and all points in between. Here's the code that creates the background brush:

private GraphicsPath backgroundPath = null;
private PathGradientBrush backgroundBrush = null;
private void MakeBackgroundBrush()
{
 // construct a points array for the entire client area
 Point[] points = {
       new Point(0, 0),
       new Point(this.ClientRectangle.Right, 0),
       new Point(this.ClientRectangle.Right, this.ClientRectangle.Bottom),
       new Point(0, this.ClientRectangle.Bottom)};

 backgroundPath = new GraphicsPath();
 // use the array of points to create a graphics path
 backgroundPath.AddLines(points);

 // use the path to create a path gradient brush
 backgroundBrush = new PathGradientBrush(backgroundPath);

 // set the center color
 backgroundBrush.CenterColor = Color.White;

 // set colors for each of the four corners
 Color[] colors = {Color.Gold, Color.Green, Color.Gold, Color.Red};

 backgroundBrush.SurroundColors = colors;
}

Note that there are two form-global variables, backgroundPath and backgroundBrush. Constructing a PathGradientBrush is expensive—it takes quite a lot of time. Since the background is going to be redrawn often, it makes sense to construct the brush only once, when the form is loaded, and then use that brush to paint the background. When you're doing animation, you'll find that it's best to pre-create whatever you can up front in order to keep the animation moving smoothly.

If you want to see what the form looks like with just the background, create a Load event and Paint event as shown here, and run the program.

private void Form1_Load(object sender, System.EventArgs e)
{
 MakeBackgroundBrush();
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
 e.Graphics.FillPath(backgroundBrush, backgroundPath);
}

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network