ARTICLE

Print Dialogs in GDI+

Posted by Mahesh Chand Articles | GDI+ & Graphics May 06, 2010
In this article I will explain about Print Dialogs in GDI+.
Reader Level:

This article has been excerpted from book "Graphics Programming with GDI+".

In the beginning of this article we said that all printing functionality is defined in the System.Drawing.Printing namespace. That statement is not entirely true. Actually, a few printing-related classes are defined in the System.Windows.Forms namespace. These classes are:

  • PrintDialog
  • PrintPriviewDialog
  • PrintPriviewControl
  • PageSetupDialog

These classes are also available as Windows Forms controls in Visual Studio .NET; we can add them to a form by dragging the control from the toolbox. The toolbox with the three print dialogs is shown in Figure 11.16.

However, adding and using these controls programmatically is even easier than using the toolbox, as we will soon see. Before you learn how to use them, let's explore their functionality.

Figure 11.16.jpg

FIGURE 11.16: Print dialogs in the Visual Studio .NET toolbox

The PrintDialog Control

The PrintDialog class represents the PrintDialog control in the .NET Framework library. This class represents a standard Windows printer dialog, which allows the user to select a printer and choose which portions of the document to print. Table 11.7 describes the PrintDialog class properties. By default, all of these properties are false when a PrintDialog object is created, and all the properties have both get and set options.

Beside the properties defined in Table 11.7, PrintDialog has on method called Reset. This method resets all options, the last selected printer, and the page settings to their default values.

Listing 11.28 creates a PrintDialog object, sets it properties, calls ShowDialog and prints the document.

LISTING 11.28: Create and using the PrintDialog control


       PrintDialog printDlg = new PrintDialog();
       PrintDocument printDoc = new PrintDocument();
       printDoc.DocumentName = "Print Document";
       printDlg.Document = printDoc;
       printDlg.AllowSelection = true;
       printDlg.AllowSomePages = true;

       //Call ShowDialog
       if (printDlg.showDialog() == DialogResult.OK)
       printDoc.Print();


TABLE 11.7: PrintDialog properties


Property

Description

AllowSelection

Indicates whether the From To Page option button is enabled.

AllowSomePages

Indicates whether the Pages option button is enabled.

Document

Identifies the PrintDocument object used to obtain printer settings.

PrinterSettings

Identifies the printer settings that the dialog box modifies.

PrintToFile

Indicates whether the Print to file check box is checked.

ShowHelp

Indicates whether the Help button is displayed.

ShowNetwork

Indicates whether the Network button is displayed.


Conclusion

Hope the article would have helped you in understanding Print Dialogs in GDI+. Read other articles on GDI+ on the website.


bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

COMMENT USING
Employers - Post Free Jobs