Click here to Skip to main content
12,945,945 members (60,212 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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);

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

1 solution

Rate this: bad
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     = = 0;
  fr.rcPage.right    = (nHorizRes/nLogPixelsX) * 1440;
  fr.rcPage.bottom   = (nVertRes/nLogPixelsY) * 1440;
  // Set up 0" margins all around.
  fr.rc              = fr.rcPage;

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,205
Maciej Los 2,453
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2017
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