Page Setup Dialog Box Print Preview

184 Creates a copy of the PrinterSettings object. The syntax of the Clone method is: Public NotOverridable Function Clone As Object _ Implements System.ICloneable.Clone GetHdevmode Returns a handle to a Windows DEVMODE device mode structure corresponding to this PrinterSettings object. The GetHdevmode method has two forms: Public Overloads Function GetHdevmode As System.IntPtr and: Public Overloads Function GetHdevmode _ ByVal pageSettings As System.Drawing.Printing.PageSettings _ As System.IntPtr The DEVMODE structure is part of the Windows API and is not discussed further in this book. GetHdevnames Returns a handle to a Windows DEVNAMES structure corresponding to this PrinterSettings object. The syntax of the GetHdevnames method is: Public Function GetHdevnames As System.IntPtr The DEVNAMES structure is part of the Windows API and is not discussed further in this book. SetHdevmode Sets properties of this PrinterSettings object based on values in the given DEVMODE structure. The syntax of the SetHdevmode method is: Public Sub SetHdevmodeByVal hdevmode As System.IntPtr The DEVMODE structure is part of the Windows API and is not discussed further in this book. SetHdevnames Sets properties of this PrinterSettings object based on values in the given DEVNAMES structure. The syntax of the SetHdevnames method is: Public Sub SetHdevnamesByVal hdevnames As System.IntPtr The DEVNAMES structure is part of the Windows API and is not discussed further in this book.

4.7.7 Page Setup Dialog Box

Windows Forms provides a common dialog box for page setup shown in Figur e 5- 18 in Chapt er 5 . Settings entered by the user in this dialog box are saved to a PageSettings object. This PageSettings object can be saved by the application and passed to the PrintDocument object prior to calling the PrintDocument objects Print method. The PrintDocument object will then use the given settings for printing. Heres the code that displays the dialog box: Private Sub ShowPageSetup 185 Display the page settings dialog box. This assumes that there is a class-level variable of type PageSettings called m_pgSettings. Dim pgSetupDlg As New PageSetupDialog pgSetupDlg.PageSettings = m_pgSettings pgSetupDlg.ShowDialog End Sub This code depends on the existence of a class-level variable of type PageSettings called m_pgSettings. Here is a suitable definition for this variable: Private member to hold the applications page settings. This could be placed in an applications main form or in another class that is accessible to the code that will need to print. Private m_pgSettings As New PageSettings Note the use of the New keyword to ensure that the PageSettings object is instantiated as soon as the enclosing class is instantiated. All that remains is to hand the PageSettings object to the PrintDocument object when its time to print. Here is the code: Private Sub PrintTheDocument Create the PrintDocument object. Dim pd As New HelloPrintDocument Hand it the PageSettings object. pd.DefaultPageSettings = m_pgSettings Create the dialog box object. 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 End Sub

4.7.8 Print Preview

Generating a print preview is trivial. An instance of the PrintDocument object is created and passed to a PrintPreviewDialog object, whose ShowDialog method is then called to show the print preview. Here is the code: Private Sub ShowPrintPreview Create the PrintDocument object. Dim pd As New HelloPrintDocument Hand it the PageSettings object. pd.DefaultPageSettings = m_pgSettings Create the print preview dialog box object. Dim dlg As New PrintPreviewDialog Pass the PrintDocument object to the dialog box object. dlg.Document = pd Show the dialog box. dlg.ShowDialog End Sub The result is shown in Figur e 4- 16 . 186 Figure 4-16. Hello, Printer in a print preview window

4.7.9 Summary of Printing