HOWTO: Use Built-In Printing Features from a Rich Edit ControlID: Q129860
|
The Rich Edit control contains built-in printing features that can be used to send formatted text to the printer with minimal effort from the programmer.
Printing from a Rich Edit control involves the use of the standard printing APIs and two Rich Edit control messages, EM_FORMATRANGE and EM_DISPLAYBAND. The EM_FORMATRANGE message can be used by itself or used in combination with the EM_DISPLAYBAND message. Included below at the end of this article is a code sample which demonstrates the usage of these messages.
void Print(HDC hPrinterDC, HWND hRTFWnd)
{
DOCINFO di;
char *szFileName = NULL;
FORMATRANGE fr;
int nHorizRes = GetDeviceCaps(hPrinterDC, HORZRES),
nVertRes = GetDeviceCaps(hPrinterDC, VERTRES),
nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX),
nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);
LONG lTextLength; // Length of document.
LONG lTextPrinted; // Amount of document printed.
// 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 1" margins all around.
fr.rc.left = fr.rcPage.left + 1440; // 1440 TWIPS = 1 inch.
fr.rc.top = fr.rcPage.top + 1440;
fr.rc.right = fr.rcPage.right - 1440;
fr.rc.bottom = fr.rcPage.bottom - 1440;
// Default the range of text to print as the entire document.
fr.chrg.cpMin = 0;
fr.chrg.cpMax = -1;
// Set up the print job (standard printing stuff here).
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(DOCINFO);
if (szFileName)
di.lpszDocName = szFileName;
else
{
di.lpszDocName = "(Untitled)";
// Do not print to file.
di.lpszOutput = NULL;
}
// Start the document.
StartDoc(hPrinterDC, &di);
// Find out real size of document in characters.
lTextLength = SendMessage ( hRTFWnd, WM_GETTEXTLENGTH, 0, 0 );
do
{
// Start the page.
StartPage(hPrinterDC);
// Print as much text as can fit on a page. The return value is
// the index of the first character on the next page. Using TRUE
// for the wParam parameter causes the text to be printed.
#ifdef USE_BANDING
lTextPrinted = SendMessage(hRTFWnd,
EM_FORMATRANGE,
FALSE,
(LPARAM)&fr);
SendMessage(hRTFWnd, EM_DISPLAYBAND, 0, (LPARAM)&fr.rc);
#else
lTextPrinted = SendMessage(hRTFWnd,
EM_FORMATRANGE,
TRUE,
(LPARAM)&fr);
#endif
// Print last page.
EndPage(hPrinterDC);
// If there is more text to print, adjust the range of characters
// to start printing at the first character of the next page.
if (lTextPrinted < lTextLength)
{
fr.chrg.cpMin = lTextPrinted;
fr.chrg.cpMax = -1;
}
}
while (lTextPrinted < lTextLength);
// Tell the control to release cached information.
SendMessage(hRTFWnd, EM_FORMATRANGE, 0, (LPARAM)NULL);
EndDoc (hPrinterDC);
}
Additional query words: \* MSONLY: kbsweptWinOS98 strohma Dec-22-1998
Keywords : kbcode kbCtrl kbNTOS351 kbNTOS400 kbWinOS2000 kbRichEdit kbGrpUser kbWinOS95 kbWinOS98 kbDSupport
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 20, 1999