SetViewportOrg (hPrnDC, xOffset, yOffset);
How to Use a Program to Calculate Print Margins
Last reviewed: May 25, 1995
Article ID: Q122037
The information in this article applies to:
Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1
Microsoft Win32 SDK, versions 3.1, 3.5, 3.51, and 4.0
SUMMARY
The Windows Software Development Kit (SDK)do
es not provide a function to calculate printer margins directly. An application can calculate this information using a combination of printer escapes and calls to the GetDeviceCaps() function in Windows or by using GetDeviceCaps() in Windows NT. This article discusses those functions and provides code fragments as illustrations.
MORE INFORMATION
An application can determine printer margins as follows:
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);
// Whendo
ing 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;
NOTE: Now, you can clip all output to the rectangle bounded by xOffset, yOffset, xOffsetOfRightMargin, and yOffsetOfBottomMargin.
For further information about margins, query in the Microsoft Knowledge Base by using these words:
GETPHYSPAGESIZE and GETPRINTINGOFFSET and GetDeviceCaps