Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
We're trying to use a CRichEditCtrl to add some formatted text to the printout of an existing MFC application.
 
Our existing code sets the output DC to have a MM_ISOTROPIC mapping mode, and we have a CRect that defines logical coordinates of the rectangle into which we'd like to draw the rich text.
 
After putting a little text into the CRichEditCtrl, we're having problems figuring out how to make this text appear at the correct position and with the correct font size.
 
// CRect rect contains the LP of the rectangle we'd like to draw into
// setup the format range attributes
fmtRange.hdc = pDC->m_hDC;
fmtRange.hdcTarget = pDC->m_hAttribDC;
fmtRange.rc = rect;
fmtRange.rcPage = rect;
fmtRange.chrg.cpMin = 0;
fmtRange.chrg.cpMax = -1;
 
m_pRichEdit->FormatRange(&fmtRange, TRUE);
m_pRichEdit->DisplayBand(&rect);
 
The effect is that the RichText is drawn, but in the wrong place on the DC, and the fonts are about half the correct size.
 
I suspect we're missing some requirement about setting a different mapping mode and/or window & viewports, but by now my head is spinning; examples seem rather thin on the ground, and are a bit contradictory.
 
I guess the first question is: does the RichTextCtrl require the DC to be set up in a specific way (a specific mapping mode?).
 
Next, the fmtRange.rc and .rcPage members confuse me. What do these rectangles correspond to? The documentation just says "rcPage: The entire area of a page on the rendering device. Units are measured in twips" & "rc: The area within the rcPage rectangle to render to. Units are measured in twips." In our situation, where we would like to render the entire RichTextCtrl contents into a small rectangular region of the page, should rcPage really be the full page coordinates or just the coordinates of the rectangle we want to draw into.
 
Can anyone offer suggestions or point me to a good example of using a CRichEditCtrl to add a box of formatted text to the display/output of an existing program?
Posted 11-Feb-13 11:30am
SteveK2341

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What you are missing is this (convert mm to flops - see 1440 reference in the code). Please note that the code below is cut-n-paste from my code, please adapt it for your situation:
 
  int    nHorizRes = GetDeviceCaps(hPrinterDC, HORZRES);
  int    nVertRes = GetDeviceCaps(hPrinterDC, VERTRES);
  int     nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);
  int     nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
 
  // Ensure the printer DC is in MM_TEXT mode.
  SetMapMode ( hPrinterDC, MM_TEXT );
 
  // Rendering to the same DC we are measuring.
  ZeroMemory(&fr, sizeof(fr));
  fr.hdc = fr.hdcTarget = hPrinterDC;
 
  // Set up the page.
  fr.rcPage.left     = fr.rcPage.top = 0;
  fr.rcPage.right    = (nHorizRes/nLogPixelsX) * 1440;
  fr.rcPage.bottom   = (nVertRes/nLogPixelsY) * 1440;
 
  // Set up 0" margins all around.
  fr.rc              = fr.rcPage;
 
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 458
1 OriginalGriff 344
2 George Jonsson 233
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100