Home > Guides > Programming > 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);
}

Related Resources

Emily Nave#TuesdayTrivia: WindowsRT, Tiles and Charms...Oh My! Win "Building Windows 8 Apps with C# and XAML" eBook by Jeremy Likness
By Emily NaveNovember 6, 2012Comments
Software developers know the thing you can count on in your career is change. Microsoft is redefining the way apps are developed -- are you ready to get on board?

Rachel BaylessInformIT's 17 Days of Giveaways
By Rachel BaylessJuly 3, 2012Comments

Get ready for a month full of giveaways. From July 9 through the end of the month, InformIT will be having 17 days of giveaways. Each week has a theme to make sure that there’s something YOU will be excited to win!

Emily Nave#TuesdayTrivia: Spotlight on WP7 (Win a copy of Sams Teach Yourself Windows Phone 7 Application Development)
By Emily NaveMay 2, 2012Comments
These days, what CAN'T a smartphone do? Microsoft is putting their own spin on things to help you experience "life in motion" when using your device. Instead of containing static application icons, the re-imagined Start screen features live Tiles showing real-time content updates.

See More Blogs