Implementing Print Margins in a Windows MFC ApplicationID: Q105444
|
To implement accurate print margins, applications should use the GETPHYSPAGESIZE and GETPRINTINGOFFSET printer escapes, as well as CDC::GetDeviceCaps() with LOGPIXELSX/LOGPIXELSY, HORZRES/VERTRES, and/or HORZSIZE/VERTSIZE.
For a detailed description of the above functions, see the Microsoft
Knowledge Base article Q11863 "Printer Page Area in Windows."
The following example demonstrates one method to implement print
margins in the SuperPad Microsoft Foundation Class (MFC) Libraries
sample. Because SuperPad already uses the m_rectDraw member of
CPrintInfo, the method demonstrated here modifies this rectangle
during OnPrint(). Note that modifying m_rectDraw during
OnBeginPrinting() or OnPrepareDC() has no effect because
CView::OnFilePrint() initializes m_rectDraw immediately after calling
OnPrepareDC().
void CPadView::CalculateMargins(CDC* pDC)
{
POINT pt;
// Start by getting the dimensions of the unprintable part of the
// page (in device units). GETPRINTINGOFFSET will tell us the left
// and upper unprintable area.
pDC->Escape(GETPRINTINGOFFSET, 0, NULL, &pt);
m_rectMargins.left = pt.x;
m_rectMargins.top = pt.y;
// To get the right and lower unprintable area, we need to take
// the entire width and height of the paper (GETPHYSPAGESIZE) and
// subtract everything else.
pDC->Escape(GETPHYSPAGESIZE, 0, NULL, &pt);
m_rectMargins.right = pt.x // total paper width
- pDC->GetDeviceCaps(HORZRES) // printable width
- m_rectMargins.left; // left unprtable margin
m_rectMargins.bottom = pt.y // total paper height
- pDC->GetDeviceCaps(VERTRES) // printable ht
- m_rectMargins.top; // rt unprtable margin
// At this point, m_rectMargins contains the widths of the
// unprintable regions on all four sides of the page in device units.
// Convert the Hi-Metric margin values from the Page Setup dialog
// to device units and subtract the unprintable part we just
// calculated. Save the results back in m_rectMargins.
// (2540 == # of Hi-Metric units in an inch)
pt.x = pDC->GetDeviceCaps(LOGPIXELSX); // dpi in X direction
pt.y = pDC->GetDeviceCaps(LOGPIXELSY); // dpi in Y direction
m_rectMargins.left = MulDiv(dlgPageSetup.m_rMargin.left, pt.x, 2540)
- m_rectMargins.left;
m_rectMargins.top = MulDiv(dlgPageSetup.m_rMargin.top, pt.y, 2540)
- m_rectMargins.top;
m_rectMargins.right = MulDiv(dlgPageSetup.m_rMargin.right, pt.x, 2540)
- m_rectMargins.right;
m_rectMargins.bottom = MulDiv(dlgPageSetup.m_rMargin.bottom, pt.y,2540)
- m_rectMargins.bottom;
// m_rectMargins now contains the values used to shrink the printable
// area of the page. Could check m_rectMargins here for negative values
// to prevent setting margins outside the printable area of the page.
// Convert to logical units and we're done!
pDC->DPtoLP(m_rectMargins);
}
void CPadView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
CEditView::OnBeginPrinting(pDC, pInfo);
CalculateMargins(pDC); // add this line
// etc.
}
...
CRect rectPage = pInfo->m_rectDraw; // existing code in OnPrint
rectPage.left += m_rectMargins.left; // add these four lines
rectPage.top += m_rectMargins.top;
rectPage.right -= m_rectMargins.right;
rectPage.bottom -= m_rectMargins.bottom;
if (!strHeader.IsEmpty()) // existing code
...
Additional query words: kbinf 1.00 1.50 2.00 2.50 no32bit noupdate
Keywords : kb16bitonly kbMFC kbPrinting kbVC
Version : 1.00 1.50
Platform : WINDOWS
Issue type :
Last Reviewed: August 3, 1999