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 program’s 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
lesson’s 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 416
596906c41.indd 468 4/7/10 12:35:26 PM
Click here to Play