18.01.2013 Views

Mastering Visual Basic .NET

Mastering Visual Basic .NET

Mastering Visual Basic .NET

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

These statements will produce output like the following when executed:<br />

Fax<br />

HPLaser<br />

\\EXPERT\XEROX<br />

The first two printers are local (Fax isn’t even a printer; it’s a driver for the fax and it’s installed<br />

by Windows). The last printer’s name is XEROX and it’s a network printer, connected to the<br />

EXPERT workstation.<br />

You can also change the current printer by setting the PrinterName property of the PrinterSettings<br />

property with either of the following statements:<br />

PrintDocument1.PrinterSettings.PrinterName = “HPLaser”<br />

PrintDocument1.PrinterSettings.PrinterName = _<br />

PrintDocument1.PrinterSettings.InstalledPrinters(1)<br />

PRINTER AND PAGE PROPERTIES<br />

Another property that needs additional explanation is the PrinterResolution object. The Printer-<br />

Resolution object provides the Kind property, which returns, or sets, the current resolution of the<br />

printer, and its value is one of the PrinterResolutionKind enumeration’s members: Custom, Draft,<br />

High, Low, and Medium. To find out the exact horizontal and vertical resolutions, read the X and Y<br />

properties of the PrinterResolution object. When you set the PrinterResolution.Kind property to<br />

Custom, you must specify the X and Y properties.<br />

Note that the PrinterResolution object is a property of the PageSettings object. The Printer-<br />

Settings object exposes a similarly named object, the PrinterResolutions property. This property is a<br />

collection that returns all resolution kinds, which are the members of the PrinterResolutionKind<br />

enumeration.<br />

Page Geometry<br />

Printing on a page is similar to generating graphics on your screen. Like the drawing surface on the<br />

monitor (the client area), the page on which you’re printing has a fixed size and resolution. The<br />

most challenging aspect of printing is the calculation of the coordinates, where each graphic element<br />

will appear. In business applications, the most common elements are strings (rendered in various<br />

fonts, styles, and sizes), lines, and rectangles, which are used as borders for tabular data. Although<br />

you can print anywhere on the page (if you want, you can start filling the page from the bottom up),<br />

we usually print one element at a time, calculate the space it takes on the page, and then print the<br />

next element next or below it. Printing code makes heavy use of the MeasureString method, and<br />

nearly all of the examples in this chapter make use of this method.<br />

The printable area is determined by the size of the paper you’re using, and in most cases it’s<br />

8.5×11 inches (keep in mind that most printers can’t print near the edge of the page). Printed pages<br />

have a margin on all four sides, and you can set a different margin on every side. The user determines<br />

the margins through the Page Setup dialog box, and your program is confined to drawing within the<br />

specified margins.<br />

You can access the current page’s margins through the Margins property of the PrintDocument1<br />

.DefaultPageSettings object. This object exposes the Left, Right, Top, and Bottom properties,<br />

which are the values of the four margins. The margins, as well as the page coordinates, are expressed in<br />

hundredths of an inch. The width of a standard letter-sized page, for example, is 8,500 units and its height<br />

709

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!