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:

  void printDocument1_QueryPageSettings(   object sender, QueryPageSettingsEventArgs e) {  // Set margins to 0.5" all the way around   // (measured in 100ths of an inch)   e.PageSettings.Margins = new Margins(50, 50, 50, 50);  }  

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

 class PageSettings : ICloneable {   // Constructors   public PageSettings();   public PageSettings(PrinterSettings printerSettings);   // Properties   public Rectangle Bounds { get; }   public bool Color { get; set; }   public bool Landscape { get; set; }   public Margins Margins { get; set; }   public PaperSize PaperSize { get; set; }   public PaperSource PaperSource { get; set; }   public PrinterResolution PrinterResolution { get; set; }   public PrinterSettings PrinterSettings { get; set; } } 

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:

 PageSetupDialog pageSetupDialog1; void InitializeComponent() {   ...   this.pageSetupDialog1 = new PageSetupDialog();   ... } void pageSetupButton_Click(object sender, EventArgs e) {   // Let the user select page settings   pageSetupDialog1.Document = printDocument1;   pageSetupDialog1.ShowDialog(); } 

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 : CommonDialog, IComponent, IDisposable {   // Constructors   public PageSetupDialog();   // Properties   public bool AllowMargins { get; set; }   public bool AllowOrientation { get; set; }   public bool AllowPaper { get; set; }   public bool AllowPrinter { get; set; }   public PrintDocument Document { get; set; }   public Margins MinMargins { get; set; }   public PageSettings PageSettings { get; set; }   public PrinterSettings PrinterSettings { get; set; }   public bool ShowHelp { get; set; }   public bool ShowNetwork { get; set; }   // Events   public event EventHandler HelpRequest;   // Methods   public virtual void Reset(); } 

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 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