打印:底部边距错误

时间:2016-09-26 14:35:16

标签: c++ winapi printing mfc richedit

我正在打印一个编辑控件 - 首先将其内容复制到一个丰富的编辑控件(至少2.0 - 可能是丰富的编辑3.0) - 然后从那里打印。

我已经完成了所有工作......但是边缘是草率/不正确。

我已经反复检查了代码,验证了调试器中的数字,而且打印页面上的边距(我可用于测试的打印机)仍然出错。

代码或多或少是来自各种网页上的丰富编辑的各种打印示例的副本 - 包括The Old New Thing,MSDN的文档和CodeProject等。

归结为(hdc用于使用用户选择的纸张来源的用户选择的打印机):

// printer dot's per inch (pixels)
const CSize dpi = { GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY) };

// paper size (in printer's dots ~ pixels)
const CSize paper = { GetDeviceCaps(hdc, PHYSICALWIDTH), GetDeviceCaps(hdc, PHYSICALHEIGHT) };

// printable size (pixels) - this defines the largest possible usable rect for this printer
const CRect rcPrintable(CPoint(GetDeviceCaps(hdc, PHYSICALOFFSETX), GetDeviceCaps(hdc, PHYSICALOFFSETY)), CSize(GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES)));

// determine the paper extents using our desired margins without violating the device's minimum margins
const CSize margin = { dpi.cx / 4, dpi.cy / 2 };    // 1/4" horizontal x 1/2" vertical margins
const CRect rcPaper(__max(rcPrintable.left, margin.cx), __max(rcPrintable.top, margin.cy), __min(rcPrintable.right, paper.cx - margin.cx), __min(rcPrintable.bottom, paper.cy - margin.cy));

// convert paper size in printer dots (pixels) to paper size in TWIPS
const CRect rcPage(MulDiv(rcPaper.left, 1440, dpi.cx), MulDiv(rcPaper.top, 1440, dpi.cy), MulDiv(rcPaper.right, 1440, dpi.cx), MulDiv(rcPaper.bottom, 1440, dpi.cy));

// build our format range data
FORMATRANGE fr;
Zero(fr);
fr.hdc = hdc;
fr.hdcTarget = hdc;

// Set page rect to physical page size in TWIPS
fr.rc = rcPage;
fr.rcPage = rcPage;

// set our target device to  the printer
m_RichEdit.SetTargetDevice(hdc, rcPage.Width());

m_RichEdit.SetSel(0, -1);   // Select the entire contents.
m_RichEdit.GetSel(fr.chrg); // Get the selection into a CHARRANGE

// track the number of pages generated
unsigned nPages = 0;

// give this job a reasonable name
CString strPrintJobName = GetPrintJobName();

// Use GDI to print successive pages
DOCINFO di = { sizeof(di) };
di.lpszDocName = strPrintJobName;
if (!StartDoc(hdc, &di))
    throw CLabeledException(_T("Unable to start the print job"));

BOOL fSuccess = TRUE;
while (fr.chrg.cpMin < fr.chrg.cpMax)
{
    // start page
    fSuccess = StartPage(hdc) > 0;
    if (!fSuccess)
        break;

    // format page
    int cpMin = m_RichEdit.FormatRange(&fr, TRUE);

    // ensure we made forward progress (avoid infinite loop!)
    fSuccess = cpMin > fr.chrg.cpMin;
    if (!fSuccess)
        break;

    // render page
    fSuccess = m_RichEdit.DisplayBand(const_cast<CRect&>(rcPage));
    if (!fSuccess)
        break;

    // end page
    fSuccess = EndPage(hdc) > 0;
    if (!fSuccess)
        break;

    // update no. pages printed
    ++nPages;

    // update our new position
    fr.chrg.cpMin = cpMin;
}

// release internal cached data from rich edit control
m_RichEdit.FormatRange(nullptr, FALSE);

// complete or abort the print job
if (fSuccess)
{
    EndDoc(hdc);
    MessageBox(FormatString(_T("Printed %u pages"), nPages));
}
else
{
    DWORD dwError = GetLastError();
    AbortDoc(hdc);
    throw CContextException(FormatString(_T("Print failed on page %u"), nPages+1), dwError);
}

我的600dpi打印机的rcPaper水平5100像素,垂直6600像素,所有尺寸减去100点,因为该打印机的物理偏移/限制最小。

所以我的1/4&#34; x 1/2&#34;边距总是大于打印机的限制,我们最终会得到一个页面= {150,300,4950,6300}(像素)。

但实际打印时,我的上限为约5/8&#34;并且底部边缘约为3/16&#34;左边距约为3/8&#34;以及约1/8&#34;的右边距。

因此,打印机本身会在发送给它的值之上添加其限制(PHYSICALOFFSETXPHYSICALOFFSETY)(或者富编辑控件正在抵消一切按这些价值观。)

......或其他事情正在发生和/或我误解了一些事情!

想法?

1 个答案:

答案 0 :(得分:0)

答案似乎是上述数学在找到匹配打印机功能和所需边距限制的“绝对”矩形方面是正确的,但要创建输出矩形,需要将物理偏移量减去为了使0,0在真实物理页面上为0,0:

// subtract out the physical offsets
// note: I see no documentation that this is what must be done, yet, it must be done
//       otherwise the entire page is off by this exact amount :(
rcPaper.left -= rcPrintable.left;
rcPaper.right -= rcPrintable.left;
rcPaper.top -= rcPrintable.top;
rcPaper.bottom -= rcPrintable.top;