Using Gradient Pens and Brushes

I l @ ve RuBoard

Pens and brushes have come a long way in a short time. GDI+ allows you to have lines and filled areas that show a gradient or sweep of colors. Modifying the code in Listing 3.5.1 will allow us to use the gradient pens or gradient fills instead of solid lines or colors.

To fill the background of the window is simple enough, we just need to specify a gradient fill brush. The LinearGradientBrush object is a member of the System.Drawing.Drawing2d namespace. Drawing a gradient fill on a line is very simple, because one of the overloads allows you to specify two Point objects, each at opposite corners of a rectangle.

Listing 3.5.2 is a selective listing of the two sections you need to change in the example from Listing 3.5.1. It contains only the modified routines OnPaint and OnPaintBackground . Remember, though, to add the declaration

 Using System.Drawing.Drwing2D; 

to your source also.

Listing 3.5.2 DrawLines2.cs: The Modified Line and Fill Code for DrawLines.cs
 1:  2: public void OnPaint(object sender, PaintEventArgs e)  3: {  4:       // the current graphics object for  5:       // this window is in the PaintEventArgs  6:  7:       Random r=new Random();  8:  9:       Color cA=Color.FromArgb(r.Next(255),r.Next(255),r.Next(255)); 10:       Color cB=Color.FromArgb(r.Next(255),r.Next(255),r.Next(255)); 11:       Point pA=new Point(r.Next(this.ClientSize.Width), 12:                      r.Next(this.ClientSize.Height)); 13:       Point pB=new Point(r.Next(this.ClientSize.Width), 14:                      r.Next(this.ClientSize.Height)); 15:       LinearGradientBrush brush = new LinearGradientBrush(pA,pB,cA,cB); 16:       Pen p=new Pen(brush,(float)r.NextDouble()*10); 17:       e.Graphics.DrawLine(p,pA,pB); 18:       p.Dispose(); 19: } 20: 21: protected override void OnPaintBackground(PaintEventArgs e) 22: { 23:       // When we resize or on the first time run 24:       // we'll paint the background, otherwise 25:       // it will be left so the lines build up 26:       if(BackgroundDirty) 27:       { 28:          BackgroundDirty = false; 29:          LinearGradientBrush gb= 30:             new LinearGradientBrush(this.ClientRectangle, 31:                                         Color.Navy, 32:                                     Color.Aquamarine, 33:                                     90); 34:          e.Graphics.FillRectangle(gb,this.ClientRectangle); 35:         gb.Dispose(); 36:       } 37: 38: } 

Figure 3.5.1 shows the application running in all it's garish glory .

Figure 3.5.1. The modified DrawLines program.

graphics/0305fig01.gif

NOTE

You might notice that the pen itself uses a brush for filling its internal color. You can also use a hatch brush or a pattern brush to draw a line. This is very powerful indeed.


I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net