The PrintPreviewControl control (and yes, the word Control is part of the control’s name, possibly to differentiate it from the PrintPreviewDialog control) displays a print preview within one of your forms. Usually, it is easier to use the PrintPreviewDialog control described in the next section to display a print preview dialog box, but you can use this control to display a preview integrated into some other part of your application.
Figure G-18 shows a form displaying the PrintPreviewControl. This control and a PrintDocument control named pdocShapes were added to the form at design time and the PrintPreviewControl control’s Document property was set to pdocShapes.
Figure G-18: The PrintPreviewControl control displays a print preview within your form.
The following code shows how the program works. The module-level variable m_PageNum indicates the next page that the pdocShapes object should draw. When it needs to generate a page, the pdocShapes object raises its PrintPage event. The event handler uses a Select Case statement to see which page it should generate, and it draws an appropriate shape. It sets e.HasMorePages appropriately and increments the page number.
Public Class Form1 ' The number of the current page. Private m_PageNum As Integer = 1 ' Generate the print document. Private Sub pdocShapes_PrintPage(ByVal sender As System.Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles pdocShapes.PrintPage Select Case m_PageNum Case 1 ' Page 1. Draw a triangle. Dim pts() As Point = { _ New Point(e.MarginBounds.X + e.MarginBounds.Width \ 2, _ e.MarginBounds.Y), _ New Point(e.MarginBounds.X + e.MarginBounds.Width, _ e.MarginBounds.Y + e.MarginBounds.Height), _ New Point(e.MarginBounds.X, _ e.MarginBounds.Y + e.MarginBounds.Height) _ } e.Graphics.DrawPolygon(Pens.Red, pts) e.HasMorePages = True m_PageNum += 1 Case 2 ' Page 2. Draw a rectangle. e.Graphics.DrawRectangle(Pens.Green, e.MarginBounds()) e.HasMorePages = True m_PageNum += 1 Case 3 ' Page 3. Draw an ellipse. e.Graphics.DrawEllipse(Pens.Blue, e.MarginBounds()) e.HasMorePages = False m_PageNum = 1 End Select End Sub End Class
That’s all the code that the program needs. When the program starts, the PrintPreviewControl control uses pdocShapes to generate the pages it needs and it displays them.
The following table describes some of the PrintPreviewControl control’s most useful properties.
Property | Purpose |
---|---|
AutoZoom | Determines whether the control automatically adjusts its Zoom property to make the display fill the control. |
Columns | The number of columns of pages that the control displays. In Figure G-18, Columns = 3. |
Document | The PrintDocument object that the control previews. |
Rows | The number of rows of pages that the control displays. In Figure G-18, Rows = 1. |
StartPage | The page number (starting with 0) displayed in the control’s first page. Your code can use this property to change the pages displayed. |
UseAntiAlias | Determines whether the control uses the system’s anti-aliasing features to smooth the preview image. Setting this to True may make the image smoother, but it may also slow down the display. |
Zoom | Determines the size of the pages within the control. The value 1.0 is full size, 0.5 is half-size, 2.0 is double size, and so forth. It’s usually easier to just set AutoZoom to True and let the control make the pages as large as possible. If you set the scale so large that the page(s) won’t fit, the control adds scroll bars so the user can see the results. |
The control’s InvalidatePreview method makes the control regenerate the print preview.
See the following section for information about the PrintPreviewDialog control. You can use that control to display a print preview without needing to build your own dialog box.