Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » GDI+ & Graphics » Getting into the DetailsCustom Controlling and the Print Controller in GDI+

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

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

Author Rank :
Page Views : 2237
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


HTML clipboard

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

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.