Drawing and Printing


As nifty as all the built-in controls are and as nicely as you can arrange them on forms using the Designer, user controls, and dialogs, sometimes you need to take things into your own hands and render the state of your form or control yourself. For example, if you need to compose a fancy About box, as shown in Figure 1.17, you'll need to handle the form's Paint event and do the drawing yourself.

Figure 1.17. Custom Drawing

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

 using System.Drawing; ... void AboutDialog_Paint(object sender, PaintEventArgs e) {  Graphics g = e.Graphics;  g.SmoothingMode = SmoothingMode.AntiAlias;   Rectangle rect = this.ClientRectangle;   int cx = rect.Width;   int cy = rect.Height;   float scale = (float)cy/(float)cx;   using( LinearGradientBrush brush =             new LinearGradientBrush( this.ClientRectangle,                                     Color.Empty,                                     Color.Empty,                                     45) ) {     ColorBlend blend = new ColorBlend();     blend.Colors =       new Color[] { Color.Red, Color.Green, Color.Blue };     blend.Positions = new float[] { 0, .5f, 1 };     brush.InterpolationColors = blend;     using( Pen pen = new Pen(brush) ) {       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);  }     }     StringFormat format = new StringFormat();     format.Alignment = StringAlignment.Center;     format.LineAlignment = StringAlignment.Center;     string s = "Ain't graphics cool?";  g.DrawString(s, this.Font, brush, rect, format);  } } 

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. If we'd like to print instead, it's a matter of getting at another Graphics object that models the printer. We can do that using the PrintDocument component and handling the events that it fires when the user requests a document to be printed. For example, we can drag the PrintDocument component from the Toolbox onto our AboutDialog form and use it to implement a Print button:

 void printButton_Click(object sender, EventArgs e) {   PrintDialog dlg = new PrintDialog();   dlg.Document = printDocument1;   if( dlg.ShowDialog() == DialogResult.OK ) {     printDocument1.Print();   } } 

Notice that before we ask the PrintDocument component to print, we use the standard PrintDialog component to ask the user which printer to use. If the user presses the OK button, we ask the document to print. Of course, it can't print on its own. Instead, it will fire the PrintPage event, asking us to draw each page:

 using System.Drawing.Printing;  ...   void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {   Graphics g = e.Graphics;   ...   }  

If you'd like to print more than one page, set the HasMorePages property of the PrintPageEventArgs class until all pages have been printed. If you'd like to be notified at the beginning and end of each print request as a whole, you'll want to handle the BeginPrint and EndPrint events. If you'd like to change settings, such as margins, paper size, landscape versus portrait mode, and so on, you'll want to handle the QueryPageSettings event.

After you have the PrintDocument events handled, WinForms makes adding print preview as easy as using the PrintPreview dialog:

 void printPreviewButton_Click(object sender, EventArgs e) {   printPreviewDialog1.Document = printDocument1;   printPreviewDialog1.ShowDialog(); } 

For more details of the printing and the drawing primitives used to render state onto a Graphics object, you'll want to read Chapter 4: Drawing Basics and Chapter 7: Printing.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

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