Calculate the left and top margins
a. Determine the upper left corner of the printable area by using the
GETPRINTINGOFFSET printer escape in Windows or by calling
GetDeviceCaps() with the PHYSICALOFFSETX and PHYSICALOFFSETY
indices in Windows NT. For example:
// Init our pt struct in case escape not supported
pt.x = 0; pt.y = 0;
// In Windows NT, the following 2 calls replace GETPRINTINGOFFSET:
// pt.x = GetDeviceCaps(hPrnDC, PHYSICALOFFSETX);
// pt.y = GetDeviceCaps(hPrnDC, PHYSICALOFFSETY);
// In Windows, use GETPRINTINGOFFSET to fill the POINT struct
// Drivers are not required to support the GETPRINTINGOFFSET escape,
// so call the QUERYESCSUPPORT printer escape to make sure
// it is supported.
Escape (hPrnDC, GETPRINTINGOFFSET, NULL, NULL, (LPPOINT) &pt);
b. Determine the number of pixels required to yield the desired margin
(x and y offsets) by calling GetDeviceCaps() using the LOGPIXELSX and
LOGPIXELSY flags.
// Figure out how much you need to offset output. Note the
// use of the "max" macro. It is possible that you are asking for
// margins that are not possible on this printer. For example, the HP
// LaserJet has a 0.25" unprintable area so we cannot get margins of
// 0.1".
xOffset = max (0, GetDeviceCaps (hPrnDC, LOGPIXELSX) *
nInchesWeWant - pt.x);
yOffset = max (0, GetDeviceCaps (hPrnDC, LOGPIXELSY) *
nInchesWeWant - pt.y);
// When doing all the output, you can either offset it by the above
// values or call SetViewportOrg() to set the point (0,0) at
// the margin offset you calculated.
SetViewportOrg (hPrnDC, xOffset, yOffset);
... all other output here ...
calculate the bottom and right margins
a. Obtain the total size of the physical page (including printable and
unprintable areas) by using the GETPHYSPAGESIZE printer escape in
Windows or by calling GetDeviceCaps() with the PHYSICALWIDTH and
PHYSICALHEIGHT indices in Windows NT.
b. Determine the number of pixels required to yield the desired right
and bottom margins by calling GetDeviceCaps using the LOGPIXELSX and
LOGPIXELSY flags.
c. Calculate the size of the printable area with GetDeviceCaps() using
the HORZRES and VERTRES flags.
The following code fragment illustrates steps 2a through 2c:
// In Windows NT, the following 2 calls replace GETPHYSPAGESIZE
// pt.x = GetDeviceCaps(hPrnDC, PHYSICALWIDTH);
// pt.y = GetDeviceCaps(hPrnDC, PHYSICALHEIGHT);
// In Windows, use GETPHYSPAGESIZE to fill the POINT struct
// Drivers are not required to support the GETPHYSPAGESIZE escape,
// so call the QUERYESCSUPPORT printer escape to make sure
// it is supported.
Escape (hPrnDC, GETPHYSPAGESIZE, NULL, NULL, (LPPOINT) &pt);
xOffsetOfRightMargin = xOffset +
GetDeviceCaps (hPrnDC, HORZRES) -
pt.x -
GetDeviceCaps (hPrnDC, LOGPIXELSX) *
wInchesWeWant;
yOffsetOfBottomMargin = yOffset +
GetDeviceCaps (hPrnDC, VERTRES) -
pt.y -
GetDeviceCaps (hPrnDC, LOGPIXELSY) *
wInchesWeWant;