466
LESSON 41 Printing with wPF
Figure 41-4 shows the result. Notice that the Windows image is centered and enlarged to fill most of
the printable area.
FIGURE 414
In addition to the PrintVisual method, the PrintDialog class provides a PrintDocument method
that prints multipage output or document objects such as
FlowDocuments or FixedDocuments.
Unfortunately these topics are fairly complex, so theyre not described here. If you need those capa-
bilities, you can find more information online at:
PrintDocument
msdn.microsoft.com/library/system.windows.controls
.printdialog.printdocument.aspx
FixedDocument
msdn.microsoft.com/library/system.windows.documents
.fixeddocument.aspx
FlowDocument
msdn.microsoft.com/library/system.windows.documents
.flowdocument.aspx
TRY IT
In this Try It, you modify the example programs described in this lesson to print an image of a
window rotated sideways so it fills even more of the printed page. Figure 41-5 shows the result.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson41 folder of the download.
596906c41.indd 466 4/7/10 12:35:26 PM
Try It
467
FIGURE 415
Lesson Requirements
Copy the PrintWindowEnlarged example program, which is available in this lesson’s download.
Modify the
PrintWindowCentered method so it rotates its result, stretching it appropriately
as shown in Figure 41-5.
Hints
This is a
lot easier than you might think. Simply add a layout transformation to the Grid to
rotate it 90 degrees.
Think about how the
Grid’s width and height will match up with the paper’s dimensions
after you rotate the
Grid.
Step-by-Step
Copy the PrintWindowEnlarged example program, which is available in this lesson’s
download.
1. This is straightforward.
Modify the
PrintWindowCentered method so it rotates its result, stretching it appropriately
as shown in Figure 41-5.
1. There are several ways you could try to do this. You could try to rotate the brush that
copies the
Window’s image into the Rectangle. You could try rotating the Rectangle.
The method that gave me the best result was to rotate the
Grid that holds everything.
596906c41.indd 467 4/7/10 12:35:26 PM
468
LESSON 41 Printing with wPF
2. After you rotate the Grid, you want its width to be the same as the printable
area’s height and vice versa. The following code shows how the program shown in
Figure 41-5 makes its
Grid control. The rest of the program’s code is the same as it is
in the PrintWindowEnlarged example program.
// Make a Grid to hold the contents.
Grid drawingGrid = new Grid();
drawingGrid.Width = printDialog.PrintableAreaHeight;
drawingGrid.Height = printDialog.PrintableAreaWidth;
drawingGrid.LayoutTransform = new RotateTransform(90);
Please select Lesson 41 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. The examples in this lesson print images of a Window but similar techniques work with any
visual object such as a
Grid, StackPanel, or TextBox.
For this exercise, build a program similar to the one
shown in Figure 41-6. Each of the programs tabs
has a Print button in the lower-right corner. When
you click the button, the program should print an
image of the
Grid control on the current tab.
You can use the Ex41-1a program available in this
lessons download as a starting point. That project
defines the user interface, but none of the code.
You can use the
PrintWindowCentered
method used by the earlier example programs
with a few changes:
Change the method’s name to
PrintGridCentered.
Instead of taking a
Window as a parameter, make the method take a Grid as a parameter.
The
Grid control doesn’t have a set width or height, so its Width and Height proper-
ties don’t return meaningful values. Use the
Grid’s ActualWidth and ActualHeight
properties instead.
To avoid repeating code, write a
PrintGrid method that takes as parameters a Grid,
title string, and
Thickness. It should display a PrintDialog and, if the user clicks
Print, it should call
PrintGridCentered to do the actual printing.
You can download the solution to this exercise from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find it in the
Lesson41 folder of the download.
FIGURE 416
596906c41.indd 468 4/7/10 12:35:26 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.159