The OnBeginPrint and OnEndPrint Methods Choosing a Printer

176 MarginBounds A rectangle that specifies the area of the page that is within the document margins i.e., the area of the page on which rendering should occur. The syntax of the MarginBounds property is: Public ReadOnly Property MarginBounds As System.Drawing.Rectangle PageBounds A rectangle that specifies the full area of the page, including the area outside the margins. The syntax of the PageBounds property is: Public ReadOnly Property PageBounds As System.Drawing.Rectangle PageSettings The page settings that apply to the page currently being printed. The syntax of the PageSettings property is: Public ReadOnly Property PageSettings As _ System.Drawing.Printing.PageSettings The PageSettings class is described later in this section.

4.7.3 The OnBeginPrint and OnEndPrint Methods

The PrintDocument class provides the OnBeginPrint and OnEndPrint methods for managing the start and finish of print jobs. The OnBeginPrint method is called prior to the first call to OnPrintPage, and the OnEndPrint method is called after the final call to OnPrintPage. The OnBeginPrint method is a good place to set up objects that will be used throughout the life of the print job—pens, brushes, and fonts, for example. The HelloPrintDocument class in Ex am ple 4- 13 instantiates a Font object during the OnPrintPage method. This is acceptable here because only one page is being printed. However, in practice documents may contain many pages, so it is better to move this code to the OnBeginPrint method. Ex am ple 4- 14 shows how the HelloPrintDocument looks when modified in this way. Example 4-14. Using OnBeginPrint and OnEndPrint to set up and tear down objects used during printing Public Class HelloPrintDocument Inherits PrintDocument Private member to hold the font that will be used for printing. Private m_fnt As Font Protected Overrides Sub OnBeginPrintByVal e As PrintEventArgs MyBase.OnBeginPrinte Create the font that will be used for printing. m_fnt = New FontArial, 10, FontStyle.Regular, _ GraphicsUnit.Point End Sub Protected Overrides Sub OnEndPrintByVal e As PrintEventArgs MyBase.OnEndPrinte Release the font. m_fnt.Dispose End Sub 177 Protected Overrides Sub OnPrintPageByVal e As PrintPageEventArgs MyBase.OnPrintPagee Draw text to the printer graphics device. Dim rect As Rectangle = e.MarginBounds e.Graphics.DrawStringHello, Printer, m_fnt, Brushes.Black, 0, 0 Indicate that there are no more pages. e.HasMorePages = False End Sub End Class

4.7.4 Choosing a Printer

The code given in Examples Ex am ple 4- 13 and Ex am ple 4- 14 merely prints to the default printer. To allow the user to select a specific printer and set other printer options, pass the PrintDocument object to a PrintDialog object and call the PrintDialog objects ShowDialog method. The ShowDialog method displays a PrintDialog dialog box shown in Figur e 5- 19 in Chapt er 5 . When the user clicks OK in the PrintDialog dialog box, the ShowDialog method sets the appropriate values in the given PrintDocument object. The PrintDocument objects Print method can then be called to print the document to the selected printer. Here is the code: Create the PrintDocument object and the dialog box object. Dim pd As New HelloPrintDocument Dim dlg As New PrintDialog Pass the PrintDocument object to the dialog box object. dlg.Document = pd Show the dialog box. Be sure to test the result so that printing occurs only if the user clicks OK. If dlg.ShowDialog = DialogResult.OK Then Print the document. pd.Print End If This code assumes the presence of the HelloPrintDocument class defined in Ex am ple 4- 13 or Ex am ple 4- 14 . Note that the HelloPrintDocument class itself does not need to be modified to support choosing a printer.

4.7.5 The PageSettings Class