CoderSource.net
Search
Rich Text Drawing, Printing Preview and Printing (GDI only)
Started by jackonlyone_new at 04-01-2006 6:31 PM. Topic has 0 replies.

Print Search « Previous Thread Next Thread »
   04-01-2006, 6:31 PM
jackonlyone_new is not online. Last active: 3/7/2006 11:42:45 PM jackonlyone_new

Top 10 Posts
Joined on 01-03-2006
Posts 6
Rich Text Drawing, Printing Preview and Printing (GDI only)

Introduction

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

I had found that there are many people asked for this question many many times, I hope this article and codes can help you. This small application demoed how to draw rich text on the canvas, and then you can print preview or print to paper  with the same look. 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:

// CRichEditCtrl for drawing.
CRichEditCtrl m_DrawRTF;

2. We modify the OnDraw method of class: CTestRichEditPrintView, the following codes are used for drawing to canvas:

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

These codes can be used for printing preview,it will works fine.But we cann't used them for printing to paper.

3. For printing to paper mode, we use the following codes:

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 start to print, and when we finish to print, we need get it back.

Click here to view online profile and report bugs.

 


   Report 
Codersource.Net » Programming » MFC Programming » Rich Text Drawing, Printing Preview and Printing (GDI only)

MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Welcome to Codersource.Net Login | Register | Faq  

Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us