Presentation is loading. Please wait.

Presentation is loading. Please wait.

STIP Chapter#10 Mastering Windows Forms Created By, Vishwesh Patel.

Similar presentations


Presentation on theme: "STIP Chapter#10 Mastering Windows Forms Created By, Vishwesh Patel."— Presentation transcript:

1 STIP Chapter#10 Mastering Windows Forms Created By, Vishwesh Patel

2 Mastering Windows Forms Printing Printing is an integral part of every complete Windows-based application Now, printing from Windows Forms with the.NET Framework means you must adopt a document-centric approach, resulting in cleaner and more manageable code While the System.Windows.Forms namespace provides seamless integration with all the standard print dialogs (such as Print Preview, Page Setup, and Print), The System.Drawing.Printing namespace offers numerous classes for extensibility and customization

3 Mastering Windows Forms These classes, and how they provide access to printing capabilities, are covered here. Other useful techniques, such as printing in the background to allow the user to continue other tasks, is also explained. you carefully keep track of the printing process using the Win32® API, to ensure that pages are printed correctly This sample Windows Forms application, shown in Figure 1, demonstrates many of the new features and capabilities you'll have access to when printing in.NET.

4 Mastering Windows Forms It allows you to choose any text document and send it to either the Print Preview dialog or to a specific printer. For demonstration purposes, I'll give you an option for selecting whether a watermark should be displayed on each page. When printing using the Print Preview dialog, you can enable or disable anti-aliasing, a built-in feature that renders text and graphics on the screen with a smoother appearance. Keep in mind, however, that this incurs the cost of reduced output speed.

5 Mastering Windows Forms

6 Let's take a tour of Windows Forms printing by first examining the quick and dirty way to send output to the printer. I will then look more closely at the right way to print from Windows Forms—using a derived PrintDocument class Using the Print Document class Printing from Windows Forms is a document- centric, event-driven process. The bulk of your effort will go into either using a generic PrintDocument object or implementing a derived PrintDocument class

7 Mastering Windows Forms Printing with a base PrintDocument class requires you to wire the class's PrintPage event to a handler method (static or instance) whose signature matches the PrintPageEventHandler delegate This event will fire when your code calls the Print method on an instance of a PrintDocument object The Graphics property of the PrintPageEventArgs object exposes a GDI+ object that encapsulates the drawing surface onto which you paint your page.

8 Mastering Windows Forms To print more than one page, you notify the underlying print controller that you have more to print. You can do this using the HasMorePages property of the PrintPageEventArgs object. Setting the HasMorePages property to true will ensure that your PrintPage event handler gets called again.

9 Mastering Windows Forms In addition, you can set up event handlers for other common print events such as BeginPrint, EndPrint, and QueryPageSettings. BeginPrint is a good place to initialize any objects (such as Fonts) that your PrintPage routine may rely on. The QueryPageSettings event fires immediately before each PrintPage event Here's an example that illustrates how to initiate a print job using the base PrintDocument class:

10 Mastering Windows Forms

11 Implementing a Derived PrintDocument Class When printing from Windows Forms, a better approach is to implement a class that inherits from the generic PrintDocument class. Doing so, you'll quickly reap the rewards of encapsulation. Instead of implementing event handlers for the BeginPrint, EndPrint, and PrintPage events, you override the OnBeginPrint, OnEndPrint, and OnPrintPage methods of the underlying PrintDocument base class

12 Mastering Windows Forms The sample printing application uses a derived PrintDocument class of type TextPrintDocument, as shown in Figure 2. The TextPrintDocument class exposes an overloaded constructor that takes a file name as an argument Alternatively, the file name can be set and read using a custom FileToPrint property. This property throws an exception when set to a nonexistent file. This class also exposes a public Boolean field, named Watermark, which enables or disables a page background graphic

13 Mastering Windows Forms

14

15

16

17 Printing: How default printing is done In MFC applications, the view class has a member function named OnDraw that contains all the drawing code. OnDraw takes a pointer to a object as a parameter. That CDC object represents the device context to receive the image produced by OnDraw

18 Mastering Windows Forms In programming for Windows, sending output to the printer is very similar to sending output to the screen. This is because the Windows graphics device interface (GDI) is hardware-independent. You can use the same GDI functions for screen display or for printing simply by using the appropriate device context. If the CDC (Change data capture) object that OnDraw receives represents the printer, OnDraw’s output goes to the printer

19 Mastering Windows Forms This explains how MFC applications can perform simple printing without requiring extra effort on your part. The framework takes care of displaying the Print dialog box and creating a device context for the printer. When the user selects the Print command from the File menu, the view passes this device context to OnDraw, which draws the document on the printer

20 Mastering Windows Forms There are significant differences between printing and screen display When you print, you have to divide the document into distinct pages and display them one at a time, rather than display whatever portion is visible in a window. As a corollary, you have to be aware of the size of the paper (whether it’s letter size, legal size, or an envelope). You may want to print in different orientations, such as landscape or portrait mode. The Microsoft Foundation Class Library can’t predict how your application will handle these issues, so it provides a protocol for you to add these capabilities in Multipage document.

21 Mastering Windows Forms Multipage Documents describes the Windows printing protocol and explains how to print documents that contain more than one page. The article covers the following topics: Printing protocol Overriding view class functions Pagination Printer pages vs. document pages Print-time pagination

22 Mastering Windows Forms The Printing Protocol To print a multipage document, the framework and view interact in the following manner. First the framework displays the Print dialog box, creates a device context for the printer, and calls the member function of the object. Then, for each page of the document, the framework calls the member function of the CDC object, instructs the view object to print the page, and calls the member function. (repeat mode) If the printer mode must be changed before starting a particular page, the view object sends the appropriate escape code by calling the member function of the CDC object. When the entire document has been printed, the framework calls the member function.

23 Mastering Windows Forms Overriding View Class Functions The class defines several member functions that are called by the framework during printing. By overriding these functions in your view class, you provide the connections between the framework’s printing logic and your view class’s printing logic. The following table lists these member functions.

24 Mastering Windows Forms CView’s Overridable Functions for Printing You can do printing-related processing in other functions as well, but these functions are the ones that drive the printing process

25 Mastering Windows Forms The following figure illustrates the steps involved in the printing process and shows where each of CView’s printing member functions are called. Additional parts of the printing process are described in the article Printing: Allocating GDI Resources.Printing: Allocating GDI Resources

26 Mastering Windows Forms Printing Loop

27 Mastering Windows Forms Pagination The framework stores much of the information about a print job in a structure. Several of the values in CPrintInfo pertain to pagination; these values are accessible as shown in the following table.

28 Mastering Windows Forms Page Number Information Stored in CPrintInfo table:

29 Mastering Windows Forms Printing: Allocating GDI Resources explains how to allocate and deallocate the Windows graphics device interface (GDI) objects needed for printing. Suppose you need to use certain fonts, pens, or other GDI objects for printing, but not for screen display. Because of the memory they require, it’s inefficient to allocate these objects when the application starts up. When the application isn’t printing a document, that memory might be needed for other purposes. It’s better to allocate them when printing begins, and then delete them when printing ends.

30 Mastering Windows Forms To allocate these GDI objects, override the member function. This function is well suited to this purpose for two reasons: the framework calls this function once at the beginning of each print job and, unlike, this function has access to the object representing the printer device driver. You can store these objects for use during the print job by defining member variables in your view class that point to GDI objects

31 Mastering Windows Forms To use the GDI objects you’ve created, select them into the printer device context in the member function. If you need different GDI objects for different pages of the document, you can examine the m_nCurPage member of the structure and select the GDI object accordingly. If you need a GDI object for several consecutive pages, Windows requires that you select it into the device context each time OnPrint is called. To deallocate these GDI objects, override the member function. The framework calls this function at the end of each print job, giving you the opportunity to deallocate printing- specific GDI objects before the application returns to other tasks

32 Mastering Windows Forms One way to compensate for the area taken by the header or footer is to use the m_rectDraw member of. Each time a page is printed, this member is initialized with the usable area of the page. If you print a header or footer before printing the body of the page, you can reduce the size of the rectangle stored in m_rectDraw to account for the area taken by the header or footer. Then OnPrint can refer to m_rectDraw to find out how much area remains for printing the body of the page

33 Mastering Windows Forms You cannot print a header, or anything else, from, because it is called before the StartPage member function of has been called. At that point, the printer device context is considered to be at a page boundary. You can perform printing only from the OnPrint member function


Download ppt "STIP Chapter#10 Mastering Windows Forms Created By, Vishwesh Patel."

Similar presentations


Ads by Google