Microsoft KB Archive/253262

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 13:52, 21 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)&gtlex, 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
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.