【问题标题】:Print: Wrong bottom margin打印:错误的下边距
【发布时间】:2016-09-26 14:35:16
【问题描述】:

我正在打印一个编辑控件 - 首先将其内容复制到富编辑控件(至少 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" x 1/2" 边距总是大于打印机的限制,我们最终得到一个页面 = { 150, 300, 4950, 6300 }(像素)。

但是当实际打印时,我得到大约 5/8" 的上边距和大约 3/16" 的下边距和大约 3/8" 的左边距和大约 1/8" 的右边距。

所以就像打印机本身在发送给它的值之上添加了它的限制(它的PHYSICALOFFSETXPHYSICALOFFSETY)(或者丰富的编辑控件通过这些值抵消了所有内容)。

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

想法?

【问题讨论】:

  • 好的 - “答案”似乎是打印机认为 PHSYCIALOFFSETX/Y 为 0,0。因此,为了使事物出现在正确的位置,必须从结果 rcPaper 中减去这些值(在我的示例中)。然后事情是 - “完美”(对于这台打印机,在所有情况下都在 1/32 以内)

标签: c++ winapi printing mfc richedit


【解决方案1】:

答案似乎是,在找到与打印机的能力和对所需边距的限制相匹配的“绝对”矩形时,上述数学是正确的,但要创建输出矩形,需要减去物理偏移量为了使 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;

【讨论】:

    猜你喜欢
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多