Click here to Skip to main content
15,891,567 members
Articles / Multimedia / GDI

Rich Text Drawing, Printing Preview and Printing to Paper (GDI only)

Rate me:
Please Sign up or sign in to vote.
2.00/5 (10 votes)
20 Mar 2006CPOL1 min read 59.2K   1.8K   22   7
To help you build Rich Text Drawing, Print Preview, Printing to paper support report application quickly and easily, includes source code
richprintfirst.gif

Introduction

"Why do the drawing and printing preview of my rich text work fine, but when I tried to print to paper, it can't work?"

I found that many people asked this question several times and I hope this article and code can help you. This small application demonstrates how to draw rich text on the canvas, and then you can print preview or print to paper with the same look. The full source codes are contained within the zip file. There is no limit, you can use it freely.

How It Works?

  1. We defined a rich text control within the header file of class: CTestRichEditPrintView, as below:
    C++
    // CRichEditCtrl for drawing.
    CRichEditCtrl m_DrawRTF;
  2. We modify the OnDraw method of class: CTestRichEditPrintView, the following codes are used for drawing to canvas:
    C++
    m_nLogPixelsX = ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSX);
    m_nLogPixelsY = ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY);
    
    fmtRange.hdcTarget = pDC->m_hAttribDC;
    fmtRange.hdc = pDC->m_hDC;
    
    CRect rcPage;
    GetClientRect(&rcPage);
    if(m_bPrint) 
    {
    rcPage = rcPrint;
    }
    
    RecalcRect(rcPage);
    fmtRange.rcPage = rcPage;
    rectText.bottom += foDefaultFontHeight;
    RecalcRect(rectText);
    fmtRange.rc = rectText;
    
    CHARRANGE chrg = { 0, -1 };
    fmtRange.chrg = chrg;
    
    m_DrawRTF.FormatRange(NULL, FALSE);
    m_DrawRTF.FormatRange(&fmtRange, TRUE);
    m_DrawRTF.FormatRange(NULL, FALSE);

    This code can be used for printing preview, it will work fine. But we can't use them for printing to paper.

  3. For printing to paper mode, we use the following code:
    C++
    szWinExt = pDC->GetWindowExt();
    szViewExt = pDC->GetViewportExt();
    nMapMode = pDC->SetMapMode(MM_ANISOTROPIC);
    int printLogx = pDC->GetDeviceCaps(LOGPIXELSX);
    int printLogy = pDC->GetDeviceCaps(LOGPIXELSY);
    pDC->SetWindowExt(printLogx, printLogy);
    pDC->SetViewportExt(CSize(printLogx, printLogy));
    
    fmtRange.hdcTarget = pDC->m_hAttribDC;
    fmtRange.hdc = pDC->m_hDC;
    
    CRect rcPage;
    GetClientRect(&rcPage);
    if(m_bPrint) 
    {
    rcPage = rcPrint;
    }
    
    RecalcRect(rcPage);
    fmtRange.rcPage = rcPage;
    rectText.bottom += foDefaultFontHeight;
    RecalcRect(rectText);
    fmtRange.rc = rectText;
    
    CHARRANGE chrg = { 0, -1 };
    fmtRange.chrg = chrg;
    
    m_DrawRTF.FormatRange(NULL, FALSE);
    m_DrawRTF.FormatRange(&fmtRange, TRUE);
    m_DrawRTF.FormatRange(NULL, FALSE);
    
    pDC->SetMapMode(nMapMode);
    pDC->SetWindowExt(szWinExt);
    pDC->SetViewportExt(szViewExt);

    We must change the map mode, window ext and viewport ext before starting to print, and when we finish printing, we need to get them back.

richprintsecond.gif

Click here to view online profile and report bugs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
Over 10 years for building MFC/C++ based application.

Comments and Discussions

 
QuestionWrong Pin
Member 300059022-Sep-11 22:16
Member 300059022-Sep-11 22:16 
GeneralCongratulations Pin
rixan28-Nov-07 18:53
rixan28-Nov-07 18:53 
Questionwhat about if i have dialog base application Pin
Chetan Sheladiya16-Jul-06 22:33
professionalChetan Sheladiya16-Jul-06 22:33 
AnswerRe: what about if i have dialog base application Pin
kellyonlyone28-Aug-07 1:09
sponsorkellyonlyone28-Aug-07 1:09 
GeneralOffset Pin
ffelixf30-Mar-06 8:51
ffelixf30-Mar-06 8:51 
GeneralUseful content, hard to read. Pin
Iain Clarke, Warrior Programmer23-Mar-06 5:57
Iain Clarke, Warrior Programmer23-Mar-06 5:57 
GeneralOK Pin
AnasHashki20-Mar-06 20:32
AnasHashki20-Mar-06 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.