Recipe 11.5. Prompting for Printed Page SettingsProblem
You want the
SolutionUse the PageSetupDialog class to prompt the user for these basic settings. The following code displays the Page Setup dialog for a basic print document: Dim pageSetup As New PageSetupDialog pageSetup.Document = New Printing.PrintDocument pageSetup. ShowDialog() Discussion
The
PageSetupDialog
's
ShowDialog()
method
The
PageSetupDialog
class encapsulates a complete form that lets the user set the page
Figure 11-3. The Page Setup dialog
Dim pageSetup As New Forms.PageSetupDialog pageSetup.PageSettings = New Printing.PageSettings pageSetup. PrinterSettings = New Printing.PrinterSettings pageSetup.PrinterSettings.PrinterName = "\MySystem\MyPrinter" pageSetup.ShowDialog() Once set, you can assign these PrinterSettings and PageSettings objects to the matching properties in your PrintDocument object: ' ----- Assumes a Printing.PrintDocument object named ' targetDocument. targetDocument.PrinterSettings = pageSetup.PrinterSettings targetDocument.DefaultPageSettings = pageSetup.PageSettings The PageSetupDialog class also comes in a Windows Formsbased control variation (see Figure 11-4). You can add this control and a related PrintDocument control to your form and display the page settings that way, but it works just the same. You assign the PrintDocument control to the PageSetupDialog 's Document property, and then call the PageSetupDialog 's ShowDialog() method. It's the exact same code that appears in this recipe's solution; only the declarations of the PageSetupDialog and PrintDocument objects have moved from your source code to the form's surface. Figure 11-4. The control version of the PageSetupDialog class
|
Recipe 11.6. Drawing Text and Graphics to a PrinterProblemYou're ready to print. How do you do it? SolutionSample code folder: Chapter 11\TextAndGraphics
Respond to the various events of the
PrintDocument
object,
Discussion
The following code sends a
Imports System.Drawing.Printing
Public Class Form1
Private WithEvents SampleDoc As Printing.PrintDocument
Private PageNumber As Integer
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
SampleDoc = New Printing.PrintDocument
SampleDoc.Print()
End Sub
Private Sub SampleDoc_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) _
Handles SampleDoc.BeginPrint
' ----- Start the page counting.
PageNumber = 0
End Sub
Private Sub SampleDoc_PrintPage(ByVal sender As Object, _
ByVal e As Printing.PrintPageEventArgs) _
Handles SampleDoc.PrintPage
' ----- Keep track of the current page.
PageNumber += 1
If (PageNumber >= 2) Then e.HasMorePages = False Else _
e.HasMorePages = True
' ----- Let's use inches, a nice easy measurement system.
e.Graphics.PageUnit = GraphicsUnit.Inch
' ----- Print some text and rectangles.
e.Graphics.DrawString("This is page " & PageNumber & _
".", New Font("Ariel", 48, FontStyle.Regular), _
Brushes.Black, 2, 2)
e.Graphics.DrawRectangle(New Pen(Color.Blue, 0.005), _
3.0!, 3.0!, 3.0!, 0.5!)
e.Graphics.DrawRectangle(New Pen(Color.Red, 0.005), _
3.25!, 3.25!, 3.0!, 0.5!)
End Sub
End Class
This sample prints two pages similar to the pages in Figure 11-5. Figure 11-5. Output from the PrintPage event
Make sure that you include the WithEvents keyword in your PrintDocument variable's declaration, or you won't be able to attach an event handler with the Handles keyword.
All printing for a document occurs in the
PrintDocument
object's
PrintPage
event. This event includes an
e.Graphics
property that exposes a full GDI+ graphics drawing surface for a single document page. Use any of the GDI+ drawing
It's up to you to determine which pages should be included in the print document, and even which page
Printing will continue until you tell it to stop. The PrintPage event's e.HasMorePages property controls everything. Set it to true if there are more pages to print after the current page or to False when you are printing the last page. Besides the PrintPage event, the PrintDocument object includes a few other useful events:
See AlsoChapter 9 includes examples of GDI+ features you can use on the graphics surface. |