printingmfcrichedit

MFC: How do you print contents of a CRichEditView?


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);
}

Solution

  • 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);
    }