Page Settings


You may have noticed that both the MarginBounds and the PageSettings properties of the PrintPageEventArgs class are read-only. Changing PageSettings on-the-fly (including the margins) requires handling the print document's QueryPageSettings event:

 
 Sub printDocument1_QueryPageSettings(sender As Object, _   e As QueryPageSettingsEventArgs)   ' Set margins to 0.5" all the way around   ' (measured in 100ths of an inch)   e.PageSettings.Margins = New Margins(50, 50, 50, 50) End Sub 

QueryPageSettingsEventArgs provides only a Cancel property and a PageSettings property. The latter is an instance of the PageSettings class:

 
 Class PageSetings   Implements ICloneable   ' Constructors   Public Overloads Sub New()   Public Overloads Sub New(printerSettings As PrinterSettings)   ' Properties   Property Bounds() As Rectangle   Property Color() As Boolean   Property Landscape() As Boolean   Property Margins() As Margins   Property PaperSize() As PaperSize   Property PaperSource() As PaperSource   Property PrinterResolution() As PrinterResolution   Property PrinterSettings() As PrinterSettings End Class 

In addition to setting the margins, the PageSettings object can be set to indicate whether color is allowed, the size and source of the paper, the printer resolution, and other printer-specific settings. You could adjust these properties programmatically during the printing process, but it's friendlier to let the user do it before the printing begins. For that, you use the PageSetupDialog component shown in Figure 7.8.

Figure 7.8. PageSetupDialog Component with Default Page Settings

Before the page setup dialog can be shown, the Document property must be set:

 
 Dim pageSetupDialog1 As PageSetupDialog Sub InitializeComponent()   ...   Me.pageSetupDialog1 = New PageSetupDialog()   ... End Sub Sub pageSetupButton_Click(sender As Object, e As EventArgs)   ' Let the user select page settings   pageSetupDialog1.Document = printDocument1   pageSetupDialog1.ShowDialog() End Sub 

When the user presses OK, the PageSettings properties are adjusted for that instance of the PrintDocument and are used at the next printing. The PageSetupDialog itself provides some useful options:

 
 Class PageSetupDialog   Inherits CommonDialog   Implements ICloneable   Implements IDisposable   ' Constructors   Public Sub New()   ' Properties   Property AllowMargins() As Boolean   Property AllowOrientation() As Boolean   Property AllowPaper() As Boolean   Property AllowPrinter() As Boolean   Property Document() As PrintDocument   Property MinMargins() As Margins   Property PageSettings() As PageSettings   Property PrinterSettings() As PrinterSettings   Property ShowHelp() As Boolean   Property ShowNetwork() As Boolean   ' Events   Event HelpRequest As EventHandler   ' Methods   MustOverride Sub Reset() End Class 

The AllowXxx properties dictate whether the dialog allows the user to change things, such as the margins or the orientation (all these properties default to true). The MinMargins property sets the minimum margins that the user can't go below. The ShowHelp property indicates whether the help button should be shown. By default it isn't shown, because there's no built-in help to show (other than the pop-up help). If you set ShowHelp to true, make sure to subscribe to the HelpRequest event so that when the user presses the help button, you can provide help. Finally, the ShowNetwork property determines whether the user can navigate the network to find a printer after pressing the Printer button ( assuming AllowPrinter is set to true).



Windows Forms Programming in Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

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