Click here to Skip to main content
13,050,450 members (82,922 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 26 Jan 2005

Solution to the preview problem while playing a metafile in a CPreviewDC

, 26 Jan 2005
Rate this:
Please Sign up or sign in to vote.
Playing a Metfile into a CPreviewDC in print preview can cause further drawing operations to not work correctly.

Metafile played direct into preview DC

Metafile played into a bitmap and then rendered using StretchDIBits()


I was checking into a print preview problem in one of our applications, where if we tried to render any clipboard output to the print preview screen, further drawing operations (but not all) on the same page would not work correctly. Typically, this would be seen as font text drawn at an incorrect angle or for clip regions in use not to work correctly, so that the was not be clipped.

I present in the demo code a way around this problem.

You can try running the demo application which demonstrates the problem. Its OnPrint() function looks like this:

void CPreviewMetafileView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    CString text;
    CSize size;
    if (!IsClipboardFormatAvailable(CF_METAFILEPICT))
        CString text("No METAFILE on clipboard");
        pDC->TextOut(0, 0, text);

    CButton *pButton = static_cast<CButton*>(GetDlgItem(IDC_PREVIEW_DIRECT));
    bool previewDirect = (pButton->GetCheck() != 0);

    // rectangle to render the metafil into
    CRect metafileRect(pInfo->m_rectDraw.Width() / 2, 
        pInfo->m_rectDraw.Height() / 2);

    if (IsClipboardFormatAvailable(CF_METAFILEPICT))
        if (previewDirect)
            // play a metafile from the clipboard if available
            GLOBALHANDLE    hGMem;
            LPMETAFILEPICT  lpMFP;
            hGMem = GetClipboardData(CF_METAFILEPICT);
            lpMFP = (LPMETAFILEPICT)GlobalLock(hGMem);
            pDC->SetViewportExt(metafileRect.Width(), metafileRect.Height());
            // render to offscreen bitmap and StretchDIBBits it
            DrawMetafileToDc(pDC, metafileRect);
    // the following output gets corrupted after a direct render
    text = "Text to be corrupted!";
    size = pDC->GetTextExtent(text);
    pDC->TextOut(0,, text);
    pDC->TextOut(0, * 2, text);
    pDC->TextOut(0, * 3, text);

As you can see, it checks to see if a metafile is available on the clipboard and renders it to the preview DC using either a direct render or an indirect render. It then adds some extra text to the output "Text to be corrupted". This text is not shown on the direct render but is on the indirect render. Although the way this corruption affects the output may be different depending on what you actually render after the metafile.

To get a metafile on the clipboard, a quick way is to copy some cells to the clipboard from Microsoft Excel.

The two functions you will be interested in the demo which you wish to use this work around are:

HANDLE CPreviewMetafileView::DDBToDIB(CBitmap& bitmap, 
                              DWORD dwCompression, CPalette* pPal)
void CPreviewMetafileView::DrawMetafileToDc(CDC * pDC, CRect targetRect)

Thats it!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Roger Allen
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url][/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

You may also be interested in...


Comments and Discussions

QuestionThe Demonstration Code Gone? Pin
wonea5-Oct-09 2:43
memberwonea5-Oct-09 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 26 Jan 2005
Article Copyright 2005 by Roger Allen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid