Microsoft KB Archive/253262: Difference between revisions
m (1 revision imported: importing part 2) |
m (Text replacement - """ to """) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 32: | Line 32: | ||
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: | 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: | ||
<blockquote><blockquote>[[../ | <blockquote><blockquote>[[../142320|Q142320]] PRB: Rich Edit Control Improperly Displays Print Preview</blockquote></blockquote> | ||
<br /> | <br /> | ||
Line 47: | Line 47: | ||
PRINTDLG pdlg; | PRINTDLG pdlg; | ||
memset( & | memset( &pdlg, 0, sizeof( PRINTDLG ) ); | ||
pdlg.lStructSize = sizeof( PRINTDLG ); | pdlg.lStructSize = sizeof( PRINTDLG ); | ||
pdlg.Flags = PD_RETURNDC; // PD_RETURNDEFAULT | pdlg.Flags = PD_RETURNDC; // PD_RETURNDEFAULT | ||
PrintDlg( & | PrintDlg( &pdlg ); | ||
return pdlg.hDC; | return pdlg.hDC; | ||
} | } | ||
Line 58: | Line 58: | ||
{ | { | ||
GETTEXTLENGTHEX gtlex = { GTL_PRECISE, CP_ACP }; | GETTEXTLENGTHEX gtlex = { GTL_PRECISE, CP_ACP }; | ||
return SendMessage( hWndRTF, EM_GETTEXTLENGTHEX, (WPARAM)& | return SendMessage( hWndRTF, EM_GETTEXTLENGTHEX, (WPARAM)>lex, 0 ); | ||
} | } | ||
Line 83: | Line 83: | ||
return NULL; | return NULL; | ||
ZeroMemory(& | ZeroMemory(&fr, sizeof(fr)); | ||
// Set up the page (convert 0.01mm to twips) | // Set up the page (convert 0.01mm to twips) | ||
fr.rcPage.top = prcMeta- | fr.rcPage.top = prcMeta->left*1440/2540; | ||
fr.rcPage.left = prcMeta- | fr.rcPage.left = prcMeta->top*1440/2540; | ||
fr.rcPage.right = prcMeta- | fr.rcPage.right = prcMeta->right*1440/2540; | ||
fr.rcPage.bottom = prcMeta- | fr.rcPage.bottom = prcMeta->bottom*1440/2540; | ||
// Set up no margins all around. | // Set up no margins all around. | ||
fr.rc = fr.rcPage; | fr.rc = fr.rcPage; | ||
Line 96: | Line 96: | ||
fr.hdc = fr.hdcTarget = hMetaDC; | fr.hdc = fr.hdcTarget = hMetaDC; | ||
// Tell the control to draw itself on our (meta) DC | // Tell the control to draw itself on our (meta) DC | ||
nTextPrinted = SendMessage(hWndRTF, EM_FORMATRANGE, TRUE, (LPARAM)& | nTextPrinted = SendMessage(hWndRTF, EM_FORMATRANGE, TRUE, (LPARAM)&fr); | ||
if( pEnd != NULL ) | if( pEnd != NULL ) | ||
*pEnd = nTextPrinted; | *pEnd = nTextPrinted; | ||
Line 120: | Line 120: | ||
hRefDC = GetPrinterDC(); | hRefDC = GetPrinterDC(); | ||
// Set up the meta RECT for 0.01mm units | // Set up the meta RECT for 0.01mm units | ||
SetRect( & | SetRect( &rcMeta, 0, 0, GetDeviceCaps(hRefDC, HORZSIZE)*100, | ||
GetDeviceCaps(hRefDC, VERTSIZE)*100 ); | GetDeviceCaps(hRefDC, VERTSIZE)*100 ); | ||
// Loop while we've not reached the end of the text in the control | // Loop while we've not reached the end of the text in the control | ||
for(nPage=0,nStart=0;nStart | for(nPage=0,nStart=0;nStart<nRTFTextLength;) | ||
{ | { | ||
// construct a file name for this page | // construct a file name for this page | ||
wsprintf(szMetaName, TEXT( | wsprintf(szMetaName, TEXT("%s%d.EMF"), szEMFFileTitleBase, nPage); | ||
// call function above to draw this portion of the RTF on the EMF | // call function above to draw this portion of the RTF on the EMF | ||
hEMF = RTFToEMF( hRefDC, szMetaName, & | hEMF = RTFToEMF( hRefDC, szMetaName, &rcMeta, hWndRTFControl, | ||
nStart, & | nStart, &nStart ); | ||
// clean up | // clean up | ||
DeleteEnhMetaFile( hEMF ); | DeleteEnhMetaFile( hEMF ); |
Latest revision as of 13:52, 21 July 2020
HOWTO: Store the Contents of an RTF Control in an EMF File |
Q253262
The information in this article applies to:
- Microsoft Win32 Application Programming Interface (API)
- Microsoft Win32 Software Development Kit (SDK)
- Microsoft Windows 98
- Microsoft Windows 95
- Microsoft Windows NT Server
- Microsoft Windows NT Workstation
- Microsoft Windows 2000 Advanced Server
- Microsoft Windows 2000 Server
- Microsoft Windows 2000 Professional
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:
Q142320 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++; } }
Additional query words: rtf rich edit control bug problem print preview EMF enhanced metafile
Keywords : kbGDI kbMetafile kbRichEdit kbSDKPlatform kbSDKWin32 _IK kbGrpDSGDI
Issue type : kbhowto
Technology : kbWinNTsearch kbWinNTWsearch kbwin2000AdvServ kbwin2000AdvServSearch kbwin2000Serv kbWinNTSsearch kbwin2000ServSearch kbwin2000Search kbwin2000ProSearch kbwin2000Pro kbWin32SDKSearch kbAudDeveloper kbWin95search kbWin98search kbSDKSearch kbWinAdvServSearch kbZNotKeyword3 kbWin32sSearch kbWin32API kbWin98
Last Reviewed: December 16, 2000 |