Earlier we saw that many Windows components work together to generate your printed output. Within your .NET code, you will also use many components (classes) to drive the printing process. Four main steps are involved (at least directly) in printing a document from your code:
Create an instance of a
PrintDocument class (or add it as a control to your form).
PrintDocument's various printer settings, either by using a
PrintDialog (or related) class/control, or by using the default or manual settings.
Add an event handler for the
PrintPage event. This event is called once for each page, and receives a
System.Drawing.Graphics object for the printer canvas. Your event handler code prints a single page, and updates a flag telling the document whether there are more pages to come.
Let's try a little code to see how this printing beast eats. Or prints. Or whatever it does. How about a simple program that prints a five-page document on the user's selected printer? The output will be a large single-digit page number, perfect for the Sesame Street set. First, let's create a new Windows Forms application, and add a single button to
ActPrint. We'll also add a
PrintDocument control (named
CountingDoc), and a
PrintDialog control (named
UserPrinter). Figure 20-5 shows the form and its supporting controls.
Figure 20-5. A program with only printing on its mind
These controls implement ...