Print Dialogs

In the beginning of this chapter 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

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.

Figure 11.16. Print dialogs in the Visual Studio .NET toolbox

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.

11.8.1 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.

Besides the properties defined in Table 11.7, PrintDialog has one 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 its properties, calls ShowDialog, and prints the document.

Listing 11.28 Creating 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.

11.8.2 The PageSetupDialog Control

The PageSetupDialog class represents the PageSetupDialog control in the .NET Framework library. This class represents a standard Windows page setup dialog that allows users to manipulate page settings, including margins and paper orientation. Users can also set a PageSettings object through PageSetupDialog's PageSettings property. Table 11.8 describes the properties of the PageSetupDialog class. All of these properties have both get and set options.

As with PrintDialog, the PageSetupDialog class has a Reset method that resets all the default values for the dialog.

Listing 11.29 creates a PageSetupDialog object, sets its properties, calls ShowDialog, and prints the document.

Table 11.8. PageSetupDialog properties

Property

Description

AllowMargins

Indicates whether the margins section of the dialog box is enabled. By default, true when a PageSetupDialog object is created.

AllowOrientation

Indicates whether the orientation section of the dialog box (landscape versus portrait) is enabled. By default, true when a PageSetupDialog object is created.

AllowPaper

Indicates whether the paper section of the dialog box (paper size and paper source) is enabled. By default, true when a PageSetupDialog object is created.

AllowPrinter

Indicates whether the Printer button is enabled. By default, true when a PageSetupDialog object is created.

Document

Identifies the PrintDocument object from which to get page settings. By default, null when a PageSetupDialog object is created.

MinMargins

Indicates the minimum margins the user is allowed to select, in hundredths of an inch. By default, null when a PageSetupDialog object is created.

PageSettings

Identifies the page settings to modify. By default, null when a PageSetupDialog object is created.

PrinterSettings

Identifies the printer settings that the dialog box will modify when the user clicks the Printer button. By default, null when a PageSetupDialog object is created.

ShowHelp

Indicates whether the Help button is visible. By default, false when a PageSetupDialog object is created.

ShowNetwork

Indicates whether the Network button is visible. By default, true when a PageSetupDialog object is created.

Listing 11.29 Creating and using the PageSetupDialog control

setupDlg = new PageSetupDialog(); printDlg = new PrintDialog(); printDoc = new PrintDocument(); printDoc.DocumentName = "Print Document"; // PageSetupDialog settings setupDlg.Document = printDoc; setupDlg.AllowMargins = false; setupDlg.AllowOrientation = false; setupDlg.AllowPaper = false; setupDlg.AllowPrinter = false; setupDlg.Reset(); if (setupDlg.ShowDialog() == DialogResult.OK) { printDoc.DefaultPageSettings = setupDlg.PageSettings; printDoc.PrinterSettings = setupDlg.PrinterSettings; }

11.8.3 The PrintPreviewDialog Control

The PrintPreviewDialog class represents the PrintPreviewDialog control in the .NET Framework library. This class represents a standard Windows print preview dialog, which allows users to preview capabilities before printing. The PrintPreviewDialog class is inherited from the Form class, which means that this dialog contains all the functionality defined in Form, Control, and other base classes.

In addition to the properties provided by the base classes, this class has its own properties. Many of these properties are very common and are provided by many controls. Table 11.9 describes a few important PrintPreviewDialog class properties. All of these properties have both get and set options.

Listing 11.30 creates a PrintPreviewDialog object, sets its properties, calls ShowDialog, and prints the document.

Listing 11.30 Creating and using the PrintPreviewDialog control

// Create a PrintPreviewDialog object PrintPreviewDialog previewDlg = new PrintPreviewDialog(); // Create a PrintDocument object PrintDocument printDoc = new PrintDocument(); // Set Document property previewDlg.Document = printDoc; previewDlg.WindowState = FormWindowState.Normal; // Show dialog previewDlg.ShowDialog();

Table 11.9. Some PrintPreviewDialog properties

Property

Description

Document

Identifies the document shown in preview.

HelpButton

Indicates whether a help button should be displayed in the caption box of the form. The default value is false.

KeyPreview

Indicates whether the form will receive key events before the event is passed to the control that has focus. The default value is false.

ShowInTaskbar

Indicates whether the form is displayed in the Windows taskbar. The default value is true.

TransparencyKey

Identifies the color that will represent transparent areas of the form.

UseAntiAlias

Indicates whether printing uses the anti-aliasing features of the operating system.

WindowState

Identifies the form's window state.

11.8.4 Print Dialogs in Action

Now let's create a Windows application. In this application you will see how to use the print dialogs in your Windows applications.

We create a Windows application and add a MainMenu control to the form. We also add four menu items and a separator to the MainMenu control. The final form looks like Figure 11.17.

Figure 11.17. The print dialog application

As usual, our first step is to add some private variables to the project, as follows:

 

// Variables private Image curImage = null; private string curFileName = null; private PrintPreviewDialog previewDlg = null; private PageSetupDialog setupDlg = null; private PrintDocument printDoc = null; private PrintDialog printDlg = null; We also add the following namespaces to the project: using System.Drawing.Printing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Drawing.Text;

 

On our form's load event, we initialize these dialogs. We also create a PrintPage event handler and add it to the PrintDocument object, as shown in Listing 11.31.

Listing 11.31 Initializing print dialogs

private void Form1_Load(object sender, System.EventArgs e) { // Create print preview dialog // and other dialogs previewDlg = new PrintPreviewDialog(); setupDlg = new PageSetupDialog(); printDlg = new PrintDialog(); printDoc = new PrintDocument(); // Set document name printDoc.DocumentName = "Print Document"; // PrintPreviewDialog settings previewDlg.Document = printDoc; // PageSetupDialog settings setupDlg.Document = printDoc; // PrintDialog settings printDlg.Document = printDoc; printDlg.AllowSelection = true; printDlg.AllowSomePages = true; // Create a PrintPage event handler printDoc.PrintPage += new PrintPageEventHandler(this.pd_Print); }

Now we add the PrintPage event handler, which calls DrawGraphicsItems as shown in Listing 11.32. We pass PrintPageEventArgs.Graphics as the only parameter to DrawGraphicsItems.

Listing 11.32 The PrintPage event handler

private void pd_Print(object sender, PrintPageEventArgs ppeArgs) { DrawGraphicsItems(ppeArgs.Graphics); }

The DrawGraphicsItems method draws an image and text on the printer or the form, depending on the Graphics object. If we pass Form.Graphics, the DrawGraphicsItems method will draw graphics objects on the form, but if we pass PrintPageEventArgs.Graphics, this method will send drawings to the printer.

The code for the DrawGraphicsItems method is given in Listing 11.33. This method also sets the smoothing mode and text qualities via the SmoothingMode and TextRenderingHint properties. After that it calls DrawImage and DrawText.

Listing 11.33 The DrawGraphicsItems method

private void DrawGraphicsItems(Graphics gObj) { // Set text and image quality gObj.SmoothingMode = SmoothingMode.AntiAlias; gObj.TextRenderingHint = TextRenderingHint.AntiAlias; if(curImage != null) { // Draw image using the DrawImage method gObj.DrawImage(curImage, AutoScrollPosition.X, AutoScrollPosition.Y, curImage.Width, curImage.Height ); } // Draw a string gObj.DrawString("Printing Dialogs Test", new Font("Verdana", 14), new SolidBrush(Color.Blue), 0, 0); }

There's just one more thing to do before we write the menu item event handlers. We call DrawGraphicsItems from the form's paint event handler, as Listing 11.34 shows. Adding this code will display the drawing on the form.

Listing 11.34 The form's paint event handler

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { DrawGraphicsItems(e.Graphics); }

Now we can write code for the menu items. The Open File menu item just lets us browse images and creates an Image object by calling the Image.FromFile method, as Listing 11.35 shows.

Listing 11.35 The Open File menu handler

private void OpenFile_Click(object sender, System.EventArgs e) { // Create a Graphics object Graphics g = this.CreateGraphics(); g.Clear(this.BackColor); // Create open file dialog OpenFileDialog openDlg = new OpenFileDialog(); // Set filter as images openDlg.Filter = "All Image files|*.bmp;*.gif;*.jpg;*.ico;"+ "*.emf,*.wmf|Bitmap Files(*.bmp;*.gif;*.jpg;"+ "*.ico)|*.bmp;*.gif;*.jpg;*.ico|"+ "Meta Files(*.emf;*.wmf)|*.emf;*.wmf"; string filter = openDlg.Filter; // Set title and initial directory openDlg.InitialDirectory = Environment.CurrentDirectory; openDlg.Title = "Open Image File"; openDlg.ShowHelp = true; // Show dialog if(openDlg.ShowDialog() == DialogResult.OK) { // Get the file name and create // Image object from file curFileName = openDlg.FileName; curImage = Image.FromFile(curFileName); } // Paint the form, which // forces a call to the paint event Invalidate(); }

The code for PrintPreviewDialog, PageSetupDialog, and PrintDialog is given in Listing 11.36. We show PrintDialog and call its PrintDocument.Print method if the user selects OK on the print dialog. We set PageSetupDialog page and printer settings when the user selects OK on the page setup dialog. For the print preview dialog, we set the UseAntiAlias property and call ShowDialog.

Listing 11.36 Print dialogs

private void PrintDialog_Click(object sender, System.EventArgs e) { if (printDlg.ShowDialog() == DialogResult.OK) printDoc.Print(); } private void PageSetupDialog_Click(object sender, System.EventArgs e) { if (setupDlg.ShowDialog() == DialogResult.OK) { printDoc.DefaultPageSettings = setupDlg.PageSettings; printDoc.PrinterSettings = setupDlg.PrinterSettings; } } private void PrintPreview_Click(object sender, System.EventArgs e) { previewDlg.UseAntiAlias = true; previewDlg.WindowState = FormWindowState.Normal; previewDlg.ShowDialog(); }

Now when we run the application and browse an image using the Open File menu item, the form looks like Figure 11.18.

Figure 11.18. Viewing an image and text

If we click on Print Preview, our program will display the print preview dialog, as shown in Figure 11.19.

Figure 11.19. The print preview dialog

As stated earlier, the page setup dialog allows us to set the page properties, including size, sources, orientation, and margins. Clicking on Print Setup on the dialog menu brings up the page setup dialog, which is shown in Figure 11.20.

Figure 11.20. The page setup dialog

Clicking on Print Dialog calls up the standard print dialog, shown in Figure 11.21.

Figure 11.21. The print dialog

We can use these dialogs as we would in any other Windows applications.

Категории