Getting into the DetailsCustom Controlling and the Print Controller in GDI+


New Page 1

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

In this article I will explain about Custom Controlling and the Print Controller in GDI+.

Custom Controlling and the Print Controller in GDI+

At this point you must feel like a printer master and have the confidence you need to write a printing application. We have covered almost every aspect of printing in .NET, but guess what! There are still a few surprises hidden in System.Drawing.Printing. You will probably never use the classes that we're going to discuss in this section, but its' not a bad idea to know about them.

So far in this article we've created a PrintDocument object, created a PrintPage event handler, and called the Print method of PrintDocument. PrintDocument took care of everything internally for us. Now we will see how to control PrintDocument objects handles printing.

The PrintController class represents print controllers in the .NET Framework library. It's an abstract base class, so its functionality comes from its three derived classes: PreviewPrintController, StandardPrintController, and PrintControllerWithStatusDialog. PrintController and its derived classes are shown schematically in Figure 11.28.

Normally PrintController is used by PrintDocument. When PrintDocument starts printing by calling the Print method, it invokes the print controller's OnStartPrint, OnEndPrint, OnStartPage, and OnEndPage methods, which determine how a printer will print the document. Usually the OnStartPrint method of PrintController is responsible for obtaining the Graphics object, which is later used by the PrintPage event handler.

The StandardPrintController class is used to send pages to the printer. We set the PrintController property of PrintDocument to PrintController. Standard- PrintController.PrintControllerWithStatusDialog adds a status dialog to the printing functionality. It shows the name of the document currently being printed. To attach PrintControllerWithStatusDialog, we set PrintDocument's PrintController property to Printcontroller.PrintControllerWithStatusDialog.

Figure 11.28.gif

FIGURE 11.28: PrintController-derived classes

The PreviewPrintController class is used for generating previews of pages being printed. Beside the methods defined in the PrintController class, PreviewPrintController provides one property (UseAntiAlias) and one method (GetPreviewPageInfo). The UseAntiAlias property indicates whether anti-aliasing will be used when the print preview is being displayed.

The GetPreviewPageInfo method captures the pages of a document as a series of images and returns them as an array called PreviewPageInfo. The PreviewPageInfo class provides print preview information for a single page. This class has two properties: Image and PhysicalSize. The Image property returns an Image object, which represents an image of the printed page, and PhysicalSize represents the size of the printed page in hundredths of an inch.

Let's write a sample application. We create a Windows application, and we add a MainMenu control, and item and a StatusBar control to the form. Our final form looks like Figure 11.29.

Figure 11.29.gif

FIGURE 11.29: Print controller test form

Before adding any code to this form, we create a MyPrintcontroller class, which is inherited from StandardPrintController. You can use the PreviewPrintController or PrintControllerWithStatusDialog classes in the same way. The code for the MyPrintController class is given in Listing 11.50. We override all four methods: OnStartPrint, OnStartPage, OnEndPrint, and OnEndPage. On these methods we notify the status bar about the status of the printing process. This information could be useful for displaying page numbers or other print status information when we're printing multipage documents.

LISTING 11.50: The MyPrintController class


        //Print controller class

        class MyPrintController : StandardPrintController
        {
            private StatusBar statusBar;
            private string str = string.Empty;
            public MyPrintController(StatusBar sBar)
                : base()
            {
                statusBar = sBar;
            }

            public override void OnStartPrint
            (PrintDocument printDoc,
            PrintEventArgs peArgs)
            {
                statusBar.Text = "OnStartPrint Called";
                base.OnStartPrint(printDoc, peArgs);
            }

            public override Graphics OnStartPAge
            (printDocument printDoc,
            PrintPageEventArgs ppea)
            {
                statusBar.Text = "OnStartPAget Called";
                return base.OnStartPage(printDoc, ppea);
            }

            public void ovveride OnEndPage(PrintDocument printDoc,
            PrintPageEventArgs ppeArgs)
            {
            statusBar.Text = "OnEndPage Called";
            base.OnEndPage (printDoc, ppeArgs);
            }

            public void overrid OnEndPrint(PrintDocument
            printDoc,PrintEventArgs peArgs)
            {
            statusBar.Text = "OnEndPrint Called";
            statusBar.Text = str;
            base.OnEndPrint (printDoc, peArgs);
            }


To call the MyPrintController class, we need to set the PrintController property of PrintDocument to invoke MyPrintController's overridden methods. Let's write a menu click event handler and set the create a PrintDocument object, set its DocumentName and PrintController properties, enable the PrintPage event handler, and call Print to print the document.

LISTING 11.51: Setting the PrintController property of PrintDocument


        private void StandardPrintControllerMenu_Click(
        object sender, System.EventArgs e)
        {
            PrintDocument printDoc = new PrintDocument();
            printDoc.DocumentName =
            "PrintController Document";
            printDoc.PrintController =
            new MyPrintController(statusBar1);
            printDoc.PrintPage +=
            new PrintPageEventHandler(PrintPageHandler);
            printDoc.Print();
        }


Listing 11.52 gives the code for the PrintPage event handler which, just draws some text on the printer.

LISTING 11.52: The PrintPage event handler


        void PrintPageHandler(object obj, PaintPageEventArgs ppeArgs)
        {
            Graphics g = ppeArgs.Graphics;
            solidBrush brush =
            new SolidBrush(Color.Red);
            Font verdana20Font =
            new Font("Verdana", 20);
            g.DrawString("Print Controller Test",
            verdana20Font, brush, 20, 20);
        }


Figure 11.30.gif

FIGURE 11.30: Print controller output

If we run the application and print, we will see that the status bar displays the status of the printing process. The first event message is shown in Figure 11.30.

You can extend this functionality to write your own custom print controllers.

Conclusion

Hope the article would have helped you in understanding Custom Controlling and the Print Controller in GDI+. Read other articles on GDI+ on the website.

book.gif