Recipe 11.7. Determining the Print Destination


Problem

You want to slightly alter the output to the print surface when the user is printing to either the preview window or the actual printer.

Solution

Access the PrintDocument object's PrintController.IsPreview property during printing to determine if you are in preview mode or not.

Discussion

The following code prints a "preview only" message at the top of each page when printing in preview mode:

 Private Sub SampleDoc_PrintPage(ByVal sender As Object, _       ByVal e As Printing.PrintPageEventArgs) _      Handles SampleDoc.PrintPage ' ----- Print a "preview only" message. If (SampleDoc.PrintController.IsPreview = True) Then _    e.Graphics.DrawString("This is a preview only.", _    New Font("Ariel", 12, FontStyle.Regular), _    Brushes.Red, 0, 0)    ' ----- Add other printing code here. End Sub 

.NET includes two different ways to determine the print-preview status of the current PrintDocument object. The PrintDocument.PrintController.IsPreview property is a simple Boolean value that can be read at any time during the printing process.

During printing, you can also access the e.PrintAction property in the PrintDocument object's QueryPageSettings event to determine the printer-output target. This property uses the three possible values of the System.Drawing.Printing.PrintAction enumeration:


PrintToFile

The print document's output is going to a disk-based file.


PrintToPreview

The print document's output is going to a preview window using the PrintPreviewDialog or PrintPreviewControl classes.


PrintToPrinter

The print document's output is going to a physical printer based on the user's printing choices.

The following code checks the PrintAction flag for a PrintDocument object named SampleDoc and takes action based on its value:

 Private Sub SampleDoc_QueryPageSettings( _       ByVal sender As Object, ByVal e As _       System.Drawing.Printing.QueryPageSettingsEventArgs) _       Handles SampleDoc.QueryPageSettings    If (e.PrintAction = PrintAction.PrintToPreview) Then       ' ----- Take preview-specific action here.    End If End Sub 

This property is available only from the QueryPageSettings event. If you want to access its value during the PrintPage event, you will have to save it in a class-level or global variable during the QueryPageSettings event.

The initial release of Version 2.0 of the .NET Framework (part of Visual Studio 2005) included a bug that caused the e.PrintAction flag to indicate the wrong value. Specifically, it never indicates PrintAction.PrintToPreview when in preview mode. Hopefully, by the time you read this recipe, a service pack or update that resolves this issue will be available for the .NET Framework.





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

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