Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C++
// 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

1 solution

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:

C++
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;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900