1

I have a simple task where I need to print the contents of a CRichEditView. No scaling needed, No print range, just print contents. Keeping it simple, I tried adding this to the message map:

ON_COMMAND(ID_FILE_PRINT, &CRichEditView::OnFilePrint)

And implementing the virtual function:

BOOL CMyRichView::OnPreparePrinting(CPrintInfo* pInfo)
{
  return DoPreparePrinting(pInfo);
}

But when it printed (this is what was actually to be printed):

BOOL CMyRichView::OnPreparePrinting(CPrintInfo* pInfo)
{
  return DoPreparePrinting(pInfo);
}

I only got half of the 'r' on return "eturn DoPreparePrinting(pInfo);". So it seems it must have to do with margins or something that the CRichEditView doesn't handle itself?

What am I missing?

TIA!!

EDIT:

I tried changing it to

BOOL CMyRichView::OnPreparePrinting(CPrintInfo* pInfo)
{
  // note a MM_TWIPS is 1/1440 of an inch.

  // create 1/2" margin which most printers support
  CSize sizepaper = GetPaperSize();
  CRect rectmargins(720, 720, sizepaper.cx - 720, sizepaper.cy - 720);

  // Need to set the margins when printing from CRichEditView
  SetMargins(rectmargins);

  // per SetMargins API reference, call wrapchanged 
  if (m_nWordWrap==WrapToTargetDevice) {
    WrapChanged();
  }

  return DoPreparePrinting(pInfo);
}

But now it just spits out tons of blank pieces of paper. I also tried it within the virtual function OnPrint(CDC* pDC, CPrintInfo* pInfo) prior to calling the base class. Same result, so deleted OnPrint() (not using it).

EDIT:

So I took that sample above from the GetPaperSize() documentation. But it was wrong. It works using this (which the documentation for SetPaperSize() uses).

BOOL CMyRichView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
  // note a MM_TWIPS is 1/1440 of an inch.

  // Need to set the margins when printing from CRichEditView
  SetMargins(CRect(720, 720, 720, 720));

  // per SetMargins API reference, call wrapchanged 
  if (m_nWordWrap==WrapToTargetDevice) {
    WrapChanged();
  }

  return __super::OnPrint(pDC, pInfo);
}
df234987
  • 513
  • 2
  • 13
  • You haven't shown printing code. – zar Feb 07 '20 at 21:01
  • That's it. It's a CRichEditView control, it supposed to handle all the details. – df234987 Feb 07 '20 at 21:47
  • 1
    Change to `SetMargins(CRect(720, 720, 720, 720));` , X2 and Y2 values are margins, not width and height. Try calling this function in `OnInitialUpdate()` Give some information about the project. Did you make it with the Wizard? Do you see a print dialog? – Barmak Shemirani Feb 07 '20 at 23:53
  • 1
    I had added the last edit above which did that and fixed it .. the mfc docs had one sample correct and one wrong. – df234987 Feb 08 '20 at 02:53
  • Why not add your own answer with your explanation and working code? – Andrew Truckle Feb 08 '20 at 14:15

1 Answers1

0

The answer is:

ON_COMMAND(ID_FILE_PRINT, &CRichEditView::OnFilePrint)


BOOL CMyRichView::OnPreparePrinting(CPrintInfo* pInfo)
{
  return DoPreparePrinting(pInfo);
}

BOOL CMyRichView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
  // note a MM_TWIPS is 1/1440 of an inch.

  // Need to set the margins when printing from CRichEditView
  SetMargins(CRect(720, 720, 720, 720));

  // per SetMargins API reference, call wrapchanged 
  if (m_nWordWrap==WrapToTargetDevice) {
    WrapChanged();
  }

  return __super::OnPrint(pDC, pInfo);
}
df234987
  • 513
  • 2
  • 13