The default coordinates for both forms and printers in VB6 are expressed in twips
. There are 1,440 twips in an inch. Twips are device-independent units. Coordinates expressed in twips remain accurate when mapping from logical inches to either screen pixels or printer dots, or to any other coordinate system. If you prefer to think in a different coordinate system, you can change the default twip-based coordinate system to inches, millimeterseven to custom unitsby setting the Printer object's ScaleMode
property, just as you can for Forms and PictureBox controls. For the purposes of this solution I'll use millimeters.
properties of the Printer object return page dimensions. These two properties always return the dimensions of the page in twips, regardless of the current coordinate system. The ScaleWidth
properties return the page dimensions in the units of the current coordinate systemin this case, millimeters. Although most developers think the values are the same, the Width
properties generally slightly larger values than the ScaleWidth
properties. The differences correspond to the width and height of the unprintable band
(the area near the edges of the paper that most laser printers ignore). Assuming that the current coordinate system is twips (ScaleMode = 1
), the Width
properties for an A4 page have the following values:
Printer.ScaleWidth = 11222
Printer.Width = 11904
The difference, 682 twips, corresponds to approximately 0.47 inches. The width of the unprintable zone for the specific printer is approximately 0.24 inches, or 6 millimeters on either side. The height of this band is slightly smaller, 482 twips. In most situations you can ignore this band, because the user-specified margins typically exceed the unprintable band dimensions.
The current position on a form, printer, or PictureBox determines where the next graphics operation will start drawing. The CurrentX
properties return or set the X and Y coordinates for the current position on an output device. When printing text, you first specify the coordinates of the first character's upper left corner with these two properties and then print the string with the Print
method. The current location is the position of the upper left corner of an imaginary rectangle that encloses the string.
The origin of the page is its upper left corner (0, 0) and coordinates increase to the right and down. However, you rarely want print on the entire page; typically, you want to print only within specified margins. Margins are an important aspect of a printout-just think of the last time you saw a printed page without a respectably sized margin. The margins, however, are not imposed by the printer or the graphics methods. It's your responsibility to respect the page margins in effect and make sure that the printer refrains from drawing elements beyond the page's margins. The left margin becomes the virtual left edge of the page, while the width of the printout is the page's width minus the left and right margins.
Page orientation is also important. Portrait
is the default orientation. When you switch to landscape orientation the page's width and height are swapped. To change orientation programmatically, you can set the Printer object's Orientation
property to one of the constants cdlPortrait
The Print Common Dialog Control
The Printer object always represents a specific printer, so output you generate through the Printer object gets directed to a specific printer and uses that printer's settings. By default, the Printer object represents the printer designated as the default printer; but you can map the Printer object to any printer your computer can access, as well as change the printout's properties through the Printer object. Typically, you'd leave the choice of which printer to use up to your users. The Common Dialog control of VB6 lets you display the Print common dialog control and read the settings selected by users. Although the Print dialog box was designed to simplify the task of customizing printout options, you can't set the printout's margins on this dialog box.
To use the Print common dialog box you must first add the Windows Common Dialog Box component to the Toolbox. To do that, right click on an empty area of the toolbox and select Components from the popup menu. Then, select the Microsoft Common Dialog Control 6.0 entry from the Components dialog box. To display the Print dialog, use statements such as the following:
CommonDialog1.CancelError = True
On Error Resume Next
' display the dialog
If Err.Number = 32755 Then Exit Sub ' user cancelled
If CommonDialog1.Orientation = cdlLandscape Then
Printer.Orientation = cdlLandscape
Printer.Orientation = cdlPortrait
The preceding code snippet displays the Print dialog then retrieves the orientation setting selected by the user and applies it to the current printer.
Note that setting the orientation on the Print dialog box doesn't affect the printout in any way. You must read the setting and set the Printer object's Orientation property accordingly. When you do so, widths and heights are swapped automatically. The constants cdlPortrait
are available to your code automatically only after you've added the Windows Common Dialog control to the toolbox. Otherwise, you must declare the two constants and define them in your code, using the value 1 for cdlPortrait
, and 2 for cdlLandscape