Basic Print Events


All print controller implementations rely on the print document's print events to gather the drawing commands into the graphics object, either to spool to the printer or to show on the screen:

 void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {   // Draw to the e.Graphics object   Graphics g = e.Graphics;  using( Font font = new Font("Lucida Console", 72) ) {  g.DrawString("Hello,\nPrinter", font, ...);  }  } 

Notice that this sample PrintPage event handler creates a font only for printing. For a single page, this code is fine, because it creates the font and then reclaims the font resources when the printing is complete. However, if we're printing more than one page, it's wasteful to create the font anew on each page. On the other hand, creating a font for printing and then caching it in a field seems wasteful if the font is never used again after the print job. What we'd really like is to be notified when a print job is started and ended so that we can have tight control over print- related resources. For this, we use the print document's BeginPrint and EndPrint events:

 Font font = null;  void printDocument1_BeginPrint(object sender, PrintEventArgs e) {  // Create font for printing   font = new Font("Lucida Console", 72);  }   void printDocument1_EndPrint(object sender, PrintEventArgs e) {  // Reclaim font   font.Dispose();   font = null;  }  

Notice that the BeginPrint and EndPrint events come with an instance of the PrintEventArgs class. The PrintEventArgs class derives from the CancelEventArgs class and provides no extra members . As you might guess, the Cancel property of the PrintEventArgs class (inherited from the CancelEventArgs base class) is used primarily by a print controller that shows a UI, such as PrintControllerWithStatusDialog, to cancel a print job.

Unlike BeginPrint and EndPrint, the PrintPage event comes with an instance of the PrintPageEventArgs class:

 class PrintPageEventArgs : EventArgs {   public bool Cancel { get; set; }   public Graphics Graphics { get; }   public bool HasMorePages { get; set; }   public Rectangle MarginBounds { get; }   public Rectangle PageBounds { get; }   public PageSettings PageSettings { get; } } 

As you've seen, the Cancel property is used to cancel a print job, and the Graphics property is used for drawing. HasMorePages defaults to false. If there are more pages to print, you set HasMorePages to true during the PrintPage handler for all pages except the last page of a multipage document:

 int totalPages = 13; int page = 1; void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {   Graphics g = e.Graphics;   g.DrawString("Hello,\nPrinter\nPage: " + page, ...);  ++page;   e.HasMorePages = (page < totalPages);  } 

This example has 13 pages, of which as many as 6 can be shown in the print preview dialog at once (as shown in Figure 7.6).

Figure 7.6. Printing Multiple Pages



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