How To Store the Contents of an RTF Control in an EMF File (253262)
The information in this article applies to:
- Microsoft Windows 2000 Server
- Microsoft Windows 2000 Advanced Server
- Microsoft Windows 2000 Professional
- Microsoft Win32 Application Programming Interface (API)
- Microsoft Platform Software Development Kit (SDK) 1.0
- Microsoft Windows 98
- Microsoft Windows 95
- Microsoft Windows NT Server
- Microsoft Windows NT Workstation
- Microsoft Windows XP Home Edition
- Microsoft Windows XP Professional
- the operating system: Microsoft Windows XP 64-Bit Edition
This article was previously published under Q253262 SUMMARY
Sometimes, you may want to store the contents of a rich edit control in a metafile. This article outlines an approach that stores this type of control's contents in enhanced metafiles, one page per metafile. This approach can be used to work around a problem in rich edit controls, which is described in the following Knowledge Base article:
142320 PRB: Rich Edit Control Improperly Displays Print Preview
MORE INFORMATION
The following code demonstrates one method to dump the contents of a rich edit control into EMF files (one per page):
// GetPrinterDC()
// returns a printer DC - uses Printer Common Dialog
HDC GetPrinterDC(void)
{
PRINTDLG pdlg;
memset( &pdlg, 0, sizeof( PRINTDLG ) );
pdlg.lStructSize = sizeof( PRINTDLG );
pdlg.Flags = PD_RETURNDC; // PD_RETURNDEFAULT
PrintDlg( &pdlg );
return pdlg.hDC;
}
// Get the length, in characters, of the text in the control
int GetRTFTextLength( HWND hWndRTF )
{
GETTEXTLENGTHEX gtlex = { GTL_PRECISE, CP_ACP };
return SendMessage( hWndRTF, EM_GETTEXTLENGTHEX, (WPARAM)>lex, 0 );
}
// RTFToEMF - Tell the control to draw itself on the EMF
// Parameters:
// hRefDC is used to create the EMF
// pszMetaFileName is the file name of the new EMF (can be NULL)
// prcMeta is the RECT used to in CreateEnhMetaFile(), in 0.01mm
// units (should not be NULL)
// hWndRTF is the control of interest
// nStart is the starting character location
// *pEnd is a pointer to an int which receives the position of
// the next character to print after this page (can be NULL)
HENHMETAFILE RTFToEMF( HDC hRefDC, LPCTSTR pszMetaFileName, LPCRECT prcMeta,
HWND hWndRTF, int nStart, int *pEnd )
{
HDC hMetaDC;
FORMATRANGE fr;
int nTextPrinted;
// Create the EMF
hMetaDC = CreateEnhMetaFile( hRefDC, pszMetaFileName, prcMeta, NULL );
if( hMetaDC == NULL )
return NULL;
ZeroMemory(&fr, sizeof(fr));
// Set up the page (convert 0.01mm to twips)
fr.rcPage.top = prcMeta->left*1440/2540;
fr.rcPage.left = prcMeta->top*1440/2540;
fr.rcPage.right = prcMeta->right*1440/2540;
fr.rcPage.bottom = prcMeta->bottom*1440/2540;
// Set up no margins all around.
fr.rc = fr.rcPage;
// Set up the range of text to print as nStart to end of document
fr.chrg.cpMin = nStart;
fr.chrg.cpMax = -1;
fr.hdc = fr.hdcTarget = hMetaDC;
// Tell the control to draw itself on our (meta) DC
nTextPrinted = SendMessage(hWndRTF, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
if( pEnd != NULL )
*pEnd = nTextPrinted;
return CloseEnhMetaFile( hMetaDC );
}
// DumpRTFToPagedEMFs - demonstrates using RTFToEMF() to create an EMF
// for each page in an RTF control
// Parameters:
// hWndRTFControl - the control
// szEMFFileTitleBase - base filename for EMF files, number is appended
void DumpRTFToPagedEMFs( HWND hWndRTFControl, LPTSTR szEMFFileTitleBase )
{
TCHAR szMetaName[MAX_PATH];
int nRTFTextLength, nStart, nPage;
HDC hRefDC;
RECT rcMeta;
HENHMETAFILE hEMF;
// First, determine how many chars are in the RTF
nRTFTextLength = GetRTFTextLength( hWndRTFControl );
// Get a reference DC (based on a printer)
hRefDC = GetPrinterDC();
// Set up the meta RECT for 0.01mm units
SetRect( &rcMeta, 0, 0, GetDeviceCaps(hRefDC, HORZSIZE)*100,
GetDeviceCaps(hRefDC, VERTSIZE)*100 );
// Loop while we've not reached the end of the text in the control
for(nPage=0,nStart=0;nStart<nRTFTextLength;)
{
// construct a file name for this page
wsprintf(szMetaName, TEXT("%s%d.EMF"), szEMFFileTitleBase, nPage);
// call function above to draw this portion of the RTF on the EMF
hEMF = RTFToEMF( hRefDC, szMetaName, &rcMeta, hWndRTFControl,
nStart, &nStart );
// clean up
DeleteEnhMetaFile( hEMF );
nPage++;
}
}
Modification Type: | Minor | Last Reviewed: | 5/3/2006 |
---|
Keywords: | kbDSWGDI2003Swept kbGDI kbhowto kbMetafile kbRichEdit KB253262 |
---|
|