Print Controllers


The name of the print document also shows up in the dialog that the print document displays during printing. The Printing dialog lets the user cancel the print job as it's being spooled to the printer, as shown in Figure 7.1.

Figure 7.1. The Printing Dialog shown by the PrintControllerWithStatusDialog

The Printing dialog is provided by a print controller. The print controller , modeled as the PrintController abstract base class and exposed via the PrintController property of the PrintDocument object, actually manages the underlying printing process and fires the events as printing progresses. The core print controller is StandardPrintController, which provides the Graphics object that wraps the printer device, causing the drawing commands to make it to the printer itself. However, the default print controller is an instance of the PrintControllerWithStatusDialog class, which is the one that shows the Printing dialog. The PrintControllerWithStatusDialog class doesn't do anything except show the dialog; it relies on StandardPrintController to communicate with the printer. In fact, creating an instance of the PrintControllerWithStatusDialog class requires an instance of the StandardPrintController class as a constructor argument. So, by default, the print control provided by the print document acts as if you'd written this code:

 void printButton_Click(object sender, EventArgs e) {  PrintController standard = new StandardPrintController();   // Can change the title from "Printing" to something else   PrintController status =   new PrintControllerWithStatusDialog(standard, "Printing");   printDocument1.PrintController = status;  printDocument1.Print(); } 

If you'd prefer to print without showing a dialog ”for example, when you're printing in the background ”you can use StandardPrintController directly:

 void printButton_Click(object sender, EventArgs e) {  // Suppress the Printing dialog   PrintController standard = new StandardPrintController();   printDocument1.PrintController = standard;  printDocument1.Print(); } 

Print Preview

Another print controller that .NET provides is PreviewPrintController, which is used for previewing a document before it's printed. Figure 7.2 shows a preview print controller being used to prepare a document for preview.

Figure 7.2. The PreviewPrintController in use by the PrintPreviewControl

PreviewPrintController is primarily used by PrintPreviewControl, which shows document previews one page at a time. PrintPreviewControl is available on the Toolbox and uses the drawing commands performed in PrintDocument's PrintPage event handler to display the client area for a standard print preview-style dialog, as shown in Figure 7.3.

Figure 7.3. The PrintPreviewControl Hosted in a Custom Form

The client area in Figure 7.3 consists of a PrintPreviewControl set to fill the client area (using DockStyle.Fill). Notice that it draws what looks like a piece of paper in miniature , showing the drawing performed by the PrintPage event handler. The PrintPreviewControl class has all kinds of interesting properties and methods for implementing a print preview-style dialog:

 class PrintPreviewControl : Control,  IComponent, IDisposable, ... {   // Constructors   public PrintPreviewControl();   // Properties   public bool AutoZoom { get; set; }   public int Columns { get; set; }   public PrintDocument Document { get; set; }   public int Rows { get; set; }   public int StartPage { get; set; }   public bool UseAntiAlias { get; set; }   public double Zoom { get; set; }   // Methods   public void InvalidatePreview();   public virtual void ResetBackColor();   public virtual void ResetForeColor(); } 

The only requirement is that the Document property be set to an instance of a PrintDocument so that the preview control can get the contents of each page to be displayed. Displaying multiple pages at once is a matter of setting the Rows and Columns properties. Figure 7.4 shows a PrintPreviewControl with Rows set to 1 and Columns set to 2.

Figure 7.4. Previewing Multiple Pages at Once in PrintPreviewControl

Displaying the next page (or the next set of pages) is a matter of setting the StartPage property, which dictates the page shown in the upper left of the control. In addition, PrintPreviewControl interprets Page Up and Page Down to move between pages.

The Zoom property is a multiplier : A Zoom of 1.0 is 100%, a Zoom of 0.5 is 50%, and a Zoom of 2.0 is 200%. The AutoZoom property is handy when PrintPreviewControl can resize. When AutoZoom is true (the default), PrintPreviewControl sets the Zoom property to scale the page (or pages) to a size as large as possible inside the control.

Finally, the UseAntiAlias property applies antialiasing to the preview image (this defaults to false to let the printer's higher resolution print smoothly without the need to antialias).

Although it's useful to be able to implement a custom print preview-style dialog with zooming, page count, and multipage support, often a "standard" print preview dialog is all that's required. In those cases, the PrintPreviewDialog component from the Toolbox is your friend. Figure 7.5 shows the PrintPreviewDialog component in action.

Figure 7.5. The PrintPreviewDialog Component

The PrintPreviewDialog component uses PrintPreviewControl and your PrintDocument instance to provide a full-featured , preview-style dialog:

  PrintPreviewDialog printPreviewDialog1;  void InitializeComponent() {   ...  this.printPreviewDialog1 = new PrintPreviewDialog();  ... } void previewButton_Click(object sender, EventArgs e) {  printPreviewDialog1.Document = printDocument1;   printPreviewDialog1.ShowDialog();  } 


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