Basic Printing


The PrintDocument class sits at the heart of the printing process in Visual Basic. The program creates an instance of this class and installs event handlers to catch its events. When the object must perform printing-related tasks, it raises events to ask the program for help.

The PrintDocument object raises four key events:

  • BeginPrint - The PrintDocument raises its BeginPrint event when it is about to start printing. The program can initialize data structures, load data, connect to databases, and perform any other chores it must do to get ready to print.

  • QueryPageSettings - Before it prints a page, the PrintDocument object raises its QueryPageSettings event. A program can catch this event and modify the document’s margins for the page that it is about to print.

  • PrintPage - The PrintDocument object raises its PrintPage event to generate a page. The program must catch this event and use the Graphics object provided by the event handler’s parameters to generate output. When it is finished, the event handler should set the value e.HasMorePages to True or False to tell the PrintDocument whether there are more pages to generate.

  • EndPrint - Finally, when it has finished printing, the PrintDocument object raises its EndPrint event. The program can catch this event to clean up any resources it used while printing. It can free data structures, close data files and database connections, and perform any other necessary cleanup chores.

Having created a PrintDocument object and its event handlers, you can do three things with it. First you can call the object’s Print method to immediately send a printout to the currently selected printer. The PrintDocument object raises its events as necessary as it generates the printout.

Second, you can set a PrintPreviewDialog control’s Document property to the PrintDocument object and then call the dialog’s ShowDialog method. The PrintPreviewDialog displays the print preview window shown in Figure 24-1, using the PrintDocument object to generate the output it displays.

image from book
Figure 24-1: The PrintPreviewDialog control lets the user zoom in and out, and view the printout’s various pages.

The preview dialog box’s printer button on the left sends the printout to the printer. Note that this makes the PrintDocument object regenerate the printout using its events, this time sending the results to the printer instead of to the print preview dialog box. The magnifying glass button displays a drop-down list where the user can select various scales for viewing the printout. The next five buttons let the user display one, two, three, four, or six of the printout’s pages at the same time. The Close button closes the dialog box and the Page up/down arrows let the user move through the printout’s pages.

The PrintPreviewControl displays a print preview much as the PrintPreviewDialog control does, except that it sits on your form. It does not provide all the buttons that the dialog box does, but it does provide methods that let you implement similar features. For example, it lets your program set the zoom level, number of columns in the display, and so forth.

The third task you can do with a PrintDocument is assign it to a PrintDialog object’s Document property and then call the dialog box’s ShowDialog method to display the dialog box shown in Figure 24-2. The user can select the printer and set its properties (for example, selecting landscape or portrait orientation). When the user clicks Print, the dialog box uses the PrintDocument object to send the printout to the printer.

image from book
Figure 24-2: The PrintDialog control lets the user send a printout to a printer.

Tip 

Your results could look different from those shown here. The print preview adjusts its appearance based on such factors as the type of printer you are using, its settings, the size of the paper you are using, and the paper’s orientation.

The following code shows how a simple printing application can send output to the printer. This is just about the smallest program that demonstrates all three uses for a PrintDocument object: printing immediately, displaying a print preview dialog box, and displaying a print dialog box.

The code declares a PrintDocument object named m_PrintDocument. It uses the WithEvents keyword, so it can easily catch the object’s events.

When the user clicks the Print Now button, the btnPrintNow_Click event handler assigns m_PrintDocument to a new PrintDocument object and calls its Print method.

If the user clicks the Print Preview button, the btnPrintPreview_Click event handler assigns m_PrintDocument to a new PrintDocument object, sets the PrintPreviewDialog object’s Document property equal to the new object, and invokes the dialog box’s ShowDialog method.

The Print Dialog button works similarly. When the user clicks this button, the btnPrintDialog_Click event handler assigns m_PrintDocument to a new PrintDocument object, sets the PrintDialog object’s Document property equal to the new object, and calls the dialog box’s ShowDialog method.

In all three cases, the PrintDocument object raises its PrintPage event when it is ready to print a page. The program’s event handler creates a 20-pixel-wide pen and uses it to draw a rectangle around the page’s margin bounds. It changes the pen so that it is dashed and inset (so it draws inside the borders of a rectangle), and then draws a rectangle around the page’s bounds. It finishes by setting e.HasMorePages to False to tell the PrintDocument that the printout is complete.

  Imports System.Drawing.Printing Public Class Form1     Private WithEvents m_PrintDocument As PrintDocument     ' Print now.     Private Sub btnPrintNow_Click(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles btnPrintNow.Click         m_PrintDocument = New PrintDocument         m_PrintDocument.Print()     End Sub     ' Display a print preview dialog.     Private Sub btnPrintPreview_Click(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles btnPrintPreview.Click         m_PrintDocument = New PrintDocument         dlgPrintPreview.Document = m_PrintDocument         dlgPrintPreview.ShowDialog()     End Sub     ' Display a print dialog.     Private Sub btnPrintDialog_Click(ByVal sender As System.Object, _      ByVal e As System.EventArgs) Handles btnPrintDialog.Click         m_PrintDocument = New PrintDocument         dlgPrint.Document = m_PrintDocument         dlgPrint.ShowDialog()     End Sub     ' Print a page with a diamond on it.     Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, _      ByVal e As System.Drawing.Printing.PrintPageEventArgs) _      Handles m_PrintDocument.PrintPage         Using the_pen As New Pen(Color.Black, 20)             e.Graphics.DrawRectangle(the_pen, e.MarginBounds)             the_pen.DashStyle = Drawing2D.DashStyle.Dash             the_pen.Alignment = Drawing2D.PenAlignment.Inset             e.Graphics.DrawRectangle(the_pen, e.PageBounds)         End Using         e.HasMorePages = False     End Sub End Class 

The PrintDocument object’s PrintPage event handler provides a parameter of type PrintPageEventArgs to let the program control the printout and to give information about the printer. This object’s PageBounds and MarginBounds properties give the location of the printer’s printable surface and the page’s margins, respectively. Typically, the printable area might be a quarter inch smaller than the paper’s physical size, and the margins might be an inch or more inside the paper’s physical size.

Figure 24-3 shows these rectangles in a print preview. The MarginBounds are drawn with a thick line, and the PageBounds are shown with a thick dashed line.

image from book
Figure 24-3: The e.PageBounds and e.MarginBounds parameters give the paper’s printable area and margins.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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