JAL Computing

C++COMProgramming .NET Mac Palm CPP/CLI Hobbies

 

Home
Up

Chapter 21 "Just Print It!"

It is surprisingly easy to add full printing support to our Draw program. Although this is certainly a detour from the topic of object oriented programming, it is just too cool to skip. 

Add Support for Printing, Preview and Page Setup

You can add "Page Setup", "Print Dialog" and "Print Preview" support to the Draw program by simply dragging the following controls onto the Draw form!

bulletPrintDocument
bulletPrintDialog
bulletPrintPreviewDialog
bulletPageSetupDialog

The IDE will add the following variables to the code page:

private System.Windows.Forms.PrintDialog printDialog1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private
System.Windows.Forms.PageSetupDialog pageSetupDialog1;

If you look at the hidden designer code you will see the addition of an event handler for printing a page and the "init" code for the print preview dialog as:

//
// printDocument1
//

this
.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);

//
// printPreviewDialog1
//

this
.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this
.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this
.printPreviewDialog1.Enabled = true;
this
.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this
.printPreviewDialog1.Location = new System.Drawing.Point(22, 29);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this
.printPreviewDialog1.Visible = false;

If you don't see the PrintPageEventHandler you may need to double click on the printDocument1 object in the Form View to generate the event code.

Here is the printDoucment1_PrintPage method that actually prints the page. This method is registered with the printDocument1.PrintPage dispatcher. When the printDocument1 fires off a print page event, the dispatcher maps the call to this method. The body of the method is identical to the body of the OnPaint method and is elegant in its simplicity!

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Graphics g= e.Graphics;
foreach (IDrawableShape ds in drawHistory)
    {
        ds.DrawYourself(g);
    }
}

Add Menu Items to Launch the Printing Dialogs

Now simply add three menu items to the File Menu Item to launch these dialogs. Here is a screen shot of the new File Menu.

If you name the menu items menuItemPrint, menuItemPageSetup and menuItemPrintPreview and then double click on the items, the IDE will generate three empty event handlers.

private void menuItemPrint_Click(object sender, System.EventArgs e)
private void menuItemPrintPreview_Click(object sender, System.EventArgs e)
private void menuItemPageSetup_Click(object sender, System.EventArgs e)

Printing the Document

When the user selects the "Print Screen" menu Item, the print dialog box is displayed.

Here is the menuItemPrint_Click event handler. First, the print dialog Document property is set to the printDocument1 object. The method launches the print dialog box and waits for a return value. It the return value is "OK", then the printDocument1.Print() event is fired. This event is eventually dispatched to the printDocument1_PrintPage method where the actual printing takes place.

private void menuItemPrint_Click(object sender, System.EventArgs e)
{
    printDialog1.AllowSomePages =
false;
    printDialog1.ShowHelp =
true;
    printDialog1.Document = printDocument1;
    DialogResult result = printDialog1.ShowDialog();
    if (result==DialogResult.OK)
    {
        
// check for invalid page range of zero
        if (printDialog1.PrinterSettings.PrintRange== PrintRange.SomePages)
        {

            if
(printDialog1.PrinterSettings.FromPage != 0)
            {
                printDocument1.Print();
            }
        }
        else
        {
            printDocument1.Print();
        }
 
    }
}

Note: If you enable AllowSomePages, you must implement the logic to print a range of user selected pages in the printPage handler. 

Print Preview

When the user selects the Print Preview... menu item, the print preview dialog box is launched.

The print preview menu item handler is fairly straight forward. The Document property is again assigned to the print document, printDocument1, and then the print preview dialog is launched. The printDocument1 object "knows" how to print the page by firing the Print() event.

private void menuItemPrintPreview_Click(object sender, System.EventArgs e)
{
    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.ShowDialog();
}

Page Setup

Finally, the project allows the user to select the page properties in the page setup dialog box.

Here is the menu item page setup handler. Since the Draw documents are colored, the Color property has been set to true.

private void menuItemPageSetup_Click(object sender, System.EventArgs e)
{
    pageSetupDialog1.Document = printDocument1;
    pageSetupDialog1.Document.DefaultPageSettings.Color =
true;
    pageSetupDialog1.ShowDialog();
}

Learn More

Here is a primer by the "Wrox Team" on printing a multi-page document with a header, body and footer:

Printing Using C#

Download the Project

That's all folks. Using the Windows Form's built in support for printing was easy. You can drag and drop components to print, print preview and page setup. You have reused the power of "Drawable" objects that contain their own state and "know" how to draw themselves to print the screen! You can download the project here.

 
Send mail to [email protected] with questions or comments about this web site. Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 © 
Last modified: 08/04/09
Hosted by www.Geocities.ws

1