Windows Forms 2.0 Programming (Microsoft .NET Development Series)

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

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

QueryPageSettingsEventArgs exposes only the Cancel and PageSettings properties. The latter is an instance of the PageSettings class:

namespace System.Drawing.Printing { class PageSettings : ICloneable { // Constructors public PageSettings(); public PageSettings(PrinterSettings printerSettings); // Properties public Rectangle Bounds { get; } public bool Color { get; set; } public float HardMarginX { get; } // New public float HardMarginY { get; } // New public bool Landscape { get; set; } public Margins Margins { get; set; } public PaperSize PaperSize { get; set; } public PaperSource PaperSource { get; set; } public RectangleF PrintableArea { get; } // New public PrinterResolution PrinterResolution { get; set; } public PrinterSettings PrinterSettings { get; set; } // Methods public object Clone(); public void CopyToHdevmode(IntPtr hdevmode); public void SetHdevmode(IntPtr hdevmode); public override string ToString(); } }

In addition to setting the margins, you can set the PageSettings object 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, from System.Windows.Forms, as shown in Figure 8.9.

Figure 8.9. PageSetupDialog Component with Default Page Settings

Before you can show the Page Setup dialog, you must set the Document property:

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

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

namespace System.Windows.Forms { sealed class PageSetupDialog : CommonDialog { // 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 bool EnableMetric { get; set; } // New 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; } // Methods public PageSetupDialog(); public override void Reset(); // Events public event EventHandler HelpRequest; } }

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 minimum margins that the user can't set smaller. EnableMetric, when true, specifies that the PageSetupDialog will display printer measurements in metric if that's what the current locale demands; by default, EnableMetric is false.

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

Категории