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: Sub printDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) ' Draw onto the e.Graphics object Dim g As Graphics = e.Graphics Dim myfont As Font = New Font("Lucida Console", 72) g.DrawString("Hello," & vbCrLf & "Printer", myfont, ...) myfont.Dispose() End Sub 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 in 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: Dim myfont As Font = Nothing Sub printDocument1_BeginPrint(sender As Object, e As PrintEventArgs) ' Create font for printing myfont = New Font("Lucida Console", 72) End Sub Sub printDocument1_EndPrint(sender As Object, e As PrintEventArgs) ' Reclaim font myfont.Dispose() myfont = Nothing End Sub 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 Inherits EventArgs Property Cancel() As Boolean Property Graphics() As Graphics Property HasMorePages() As Boolean Property MarginBounds() As Rectangle Property PageBounds() As Rectangle Property PageSettings() As PageSettings End Class 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: Dim totalPages As Integer = 13 Dim page As Integer = 1 Sub printDocument1_PringPage(sender As Object, e As PrintPageEventArgs) Dim g As Graphics = e.Graphics g.DrawString("Hello" & vbCrLf & "Printer" & vbCrLf & _ "Page: " & page, ...) page += 1 ' Test for last page e.HasMorePages = (page < totalPages) End Sub 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
|