Microsoft Knowledge Base Email Alertz

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 wor

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 253262 - Last Review: February 22, 2007 - Revision: 2.6

How To Store the Contents of an RTF Control in an EMF File

System TipThis article applies to a different version of Windows than the one you are using. Content in this article may not be relevant to you. Visit the Windows Vista Solution Center
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  (http://kbalertz.com/Feedback.aspx?kbNumber=142320/EN-US/ ) 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++;
    }
}
				

APPLIES TO
  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional Edition
  • Microsoft Win32 Application Programming Interface
  • Microsoft Platform Software Development Kit-January 2000 Edition
  • Microsoft Windows 98 Standard Edition
  • Microsoft Windows 95
  • Microsoft Windows NT Server 3.51
  • Microsoft Windows NT Workstation 4.0 Developer Edition
  • Microsoft Windows XP Home Edition
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
Keywords: 
kbhowto kbmetafile kbrichedit kbgdi kbdswgdi2003swept KB253262
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate