Previous Section  < Day Day Up >  Next Section

9.4. Summary

This chapter has focused on using the GDI+ library to display and print text. The first section explained how to create and use font families and font classes. The emphasis was on how to construct fonts and understand the elements that comprise a font by looking at font metrics.

After a font has been created, the Graphics.DrawString method is used to draw a text string to a display or printer. Its many overloads permit text to be drawn at specific coordinates or within a rectangular area. By default, text printed in a rectangle is left justified and wrapped to the next line when it hits the bounds of the rectangle. This default formatting can be overridden by passing a StringFormat object to the DrawString method. The StringFormat class is the key to .NET text formatting. It is used to justify text, specify how text is truncated, and set tab stops for creating columnar output.

GDI+ provides several classes designed to support printing to a printer. These include the following:

  • PrintDocument. Sends output to the printer. Its Print method initiates the printing process and triggers the BeginPrint, QueryPageSettingsEvent, PrintPage, and EndPrint events. The event handler for the PrintPage event contains the logic for performing the actual printing.

  • PrintPreviewDialog. Enables output to be previewed before printing.

  • PrinterSettings. Has properties that specify the page range to be printed, the list of available printers, and the name of the target printer. These values correspond to those a user can select on the PrintDialog dialog box.

  • DefaultPageSettings. Has properties that set the bounds of a page, the orientation (landscape or portrait), the margins, and the paper size. These values correspond to those selected on the PageSetupDialog dialog box.

An example for printing a columnar report demonstrated how these classes can be combined to create an application that provides basic report writing. As a final example, we illustrated how the shortcomings of the PrintDocument class can be overcome by creating a custom PrintDocument class that preserves data encapsulation.

    Previous Section  < Day Day Up >  Next Section