Drawing


As nifty as all the built-in controls are, and as nicely as you can arrange them, sometimes you need to take things into your own hands and render the state of your form or control yourself. For example, if you want to compose a fancy About dialog, as shown in Figure 1.23, you must handle the form's Paint event and do the drawing yourself.

Figure 1.23. Custom Drawing (See Plate 2)


The following is the Paint event-handling code to fill the inside of the About dialog:

// AboutDialog.cs using System.Drawing; using System.Drawing.Drawing2D; partial class AboutDialog : Form {   ...   void AboutDialog_Paint(object sender, PaintEventArgs e) {     Graphics g = e.Graphics;     g.SmoothingMode = Smoothing Mode.AntiAlias;     Rectangle rect = this.ClientRectangle;     int cx = rect.Width;     int cy = rect.Height;     float scale = (float)cy / (float)cx;     LinearGradientBrush brush =       new LinearGradientBrush(this.ClientRectangle,                               Color.Empty,                               Color.Empty,                               45);     try {       ColorBlend blend = new ColorBlend();       blend.Colors =         new Color[] { Color.Red, Color.Green, Color.Blue };       blend.Positions = new float[] { 0, .5f, 1 };       brush.InterpolationColors = blend;       Pen pen = new Pen(brush);       try {         for( int x = 0; x < cx; x += 7 ) {           g.DrawLine(pen, 0, x * scale, cx - x, 0);           g.DrawLine(pen, 0, (cx - x) * scale, cx - x, cx * scale);           g.DrawLine(pen, cx - x, 0 * scale, cx, (cx - x) * scale);           g.DrawLine(pen, cx - x, cx * scale, cx, x * scale);         }       }       finally {         pen.Dispose();       }       StringFormat format = new StringFormat();       try {         format.Alignment = StringAlignment.Center;         format.LineAlignment = StringAlignment.Center;         string s = "Ain't graphics cool?";         g.DrawString(s, this.Font, brush, rect, format);       }       finally {         format.Dispose();       }     }     finally {       brush.Dispose();     }   } }


Notice the use of the Graphics object from the PaintEventArgs passed to the event handler. This provides an abstraction around the specific device we're drawing on, which we do with constructs like pens, brushes, shapes, and text. All this and more are explored in Chapter 5: Drawing Basics, Chapter 6: Drawing Text, and Chapter 7: Advanced Drawing.

You may be wondering what the try-finally blocks are for. Because the pen and brush objects hold underlying resources managed by Windows, we're responsible for releasing the resources when we're finished, even in the face of an exception. Like many classes in .NET, the Pen and Brush classes implement the IDisposable interface, which serves as a signal for an object's client to call the IDisposable Dispose method when it's finished with an object. This lets the object know that it's time to clean up any unmanaged resources it's holding, such as a file, a database connection, or a graphics resource.

To simplify things in C#, you can replace the try-finally block with a using block (shown here for the Brush object):

// AboutDialog.cs  using System.Drawing;  using System.Drawing.Drawing2D;  partial class AboutDialog : Form {     ...     void AboutDialog_Paint(object sender, PaintEventArgs e) {       using( LinearGradientBrush brush =                 new LinearGradientBrush(this.ClientRectangle,                                         Color.Empty,                                         Color.Empty,                                         45) ) {       ...       // Wrap Pen and StringFormat usage in "using" blocks, too       ...     } // brush.Dispose called automatically   } }


The C# using block instructs the compiler to wrap the code it contains in a try-finally block and call the IDisposable Dispose method at the end of the block for objects created as part of the using clause. This is a convenient shortcut for C# programmers, a good practice to get into, and something you'll see used extensively in the rest of this book.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

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