DetailsCustom Controlling and Print Controller in VB.NET

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

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.

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

Figure2011_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
        Inherits StandardPrintController
        Private statusBar As StatusBar
        Private str As String = String.Empty
        Public Sub New(ByVal sBar As StatusBar)
            statusBar = sBar
        End Sub

        Public Overrides Property OnStartPrint() As Void
        End Property
        (PrintDocument printDoc,
        Private Function peArgs)() As PrintEventArgs
            statusBar.Text = "OnStartPrint Called"
        MyBase.OnStartPrint(printDoc, peArgs)
        End Function

        Public Overrides Property OnStartPAge() As Graphics
        End Property
        (printDocument printDoc,
        Private Function ppea)() As PrintPageEventArgs
            statusBar.Text = "OnStartPAget Called"
        Return MyBase.OnStartPage(printDoc, ppea)
        End Function

        Public ovveride Property printDoc,() As OnEndPage(PrintDocument
        End Property
        Private Function ppeArgs)() As PrintPageEventArgs
            statusBar.Text = "OnEndPage Called"
        MyBase.OnEndPage(printDoc, ppeArgs)
        End Function

        Public Property OnEndPrint(PrintDocument() As overrid
        End Property
        printDoc,PrintEventArgs peArgs)
{
       statusBar.Text = "OnEndPrint Called"
       statusBar.Text = str
       MyBase.OnEndPrint (printDoc, peArgs)
    End Class
{
    Private statusBar As StatusBar
    Private str As String = String.Empty
    Private Function MyPrintController(ByVal sBar As StatusBar) As Public
        statusBar = sBar
    End Function

    Public Overrides Property OnStartPrint() As Void
    End Property
    (PrintDocument printDoc,
    Private Function peArgs)() As PrintEventArgs
        statusBar.Text = "OnStartPrint Called"
    MyBase.OnStartPrint(printDoc, peArgs)
    End Function

    Public Overrides Property OnStartPAge() As Graphics
    End Property
    (printDocument printDoc,
    Private Function ppea)() As PrintPageEventArgs
        statusBar.Text = "OnStartPAget Called"
    Return MyBase.OnStartPage(printDoc, ppea)
    End Function

    Public ovveride Property printDoc,() As OnEndPage(PrintDocument
    End Property
    Private Function ppeArgs)() As PrintPageEventArgs
        statusBar.Text = "OnEndPage Called"
    MyBase.OnEndPage(printDoc, ppeArgs)
    End Function

    Public Property OnEndPrint(PrintDocument() As overrid
    End Property
    Private Function peArgs)() As printDoc,PrintEventArgs
        statusBar.Text = "OnEndPrint Called"
        statusBar.Text = str
    MyBase.OnEndPrint(printDoc, peArgs)
    End Function
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 Sub StandardPrintControllerMenu_Click(ByVal sender As Object, ByVal e As
    System.EventArgs)
    Dim printDoc As New PrintDocument()
        printDoc.DocumentName = "PrintController Document"
        printDoc.PrintController = New MyPrintController(statusBar1)
        printDoc.PrintPage += New PrintPageEventHandler(PrintPageHandler)
        printDoc.Print()
    End Sub

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


    Private Sub PrintPageHandler(ByVal obj As Object, ByVal ppeArgs As PaintPageEventArgs)
    Dim g As Graphics = ppeArgs.Graphics
    Dim brush As solidBrush = New SolidBrush(Color.Red)
    Dim verdana20Font As New Font("Verdana", 20)
        g.DrawString("Print Controller Test", verdana20Font, brush, 20, 20)
    End Sub

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.