Article ID: 129860
Article Last Modified on 7/8/2005
void Print(HDC hPrinterDC, HWND hRTFWnd)
{
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);
}
Keywords: kbhowto kbrichedit kbctrl KB129860