Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / MFC
Article

Using a CStatic control to display an EMF image

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
31 Aug 20022 min read 66.4K   2.1K   21   1
Displaying enhanced metafiles in dialogs

Sample Image

Introduction

There are two possibilities to save the information of images: Bitmaps ('*.bmp'), that describe each single pixel of an image, or metafiles ('*.wmf' or '*.emf'), that describe how to draw an image using complex instructions like lines or circles instead. In the world of windows, WMF or EMF files are using directly the instruction set of GDI, the windows grafic engine. If a metafile is (dis-)played, this GDI instruction chain is called command after command using vectors to specify the location of each single graphic element. And because it is very easy to rescale vectors, its very easy to rescale metafile pictures without any loss of information. This is one important advantage of metafiles. The other advantage is the low usage of memory if the image is not too complex. So there are some good reasons to use metafiles instead of bitmaps for images which are simple to describe. But how to do this programming windows?

The MFC control CStatic supports metafiles. With the method SetEnhMetaFile(HENHMETAFILE hMetaFile) a metafile is selected and played each time the control is displayed, scaled in it's size to the frame of the control. The only problem: Where comes the handle for the metafile from? To generate metafiles a standard drawing program is required. Most of them can export to EMF file format. This file can be imported inside a MFC project as a custom resource using the resource editor, for example as an 'emf' resource. At runtime, like any resource, the metafile resource must be located by its identifier and loaded into the memory context of the process. The example class CEnhMetaFileCtrl with its method SetEnhMetaFileToCtrl(UINT metaID, HMODULE hModule/*= NULL*/) shows how to do this. The class extends CStatic accordingly.

CEnhMetaFileCtrl::CEnhMetaFileCtrl() 
{ 
    m_hMetaFile = NULL;
}


CEnhMetaFileCtrl::~CEnhMetaFileCtrl()
{ 
    if (m_hMetaFile) 
        DeleteEnhMetaFile(m_hMetaFile); 
}


BOOL CEnhMetaFileCtrl::SetEnhMetaFileToCtrl(UINT metaID, HMODULE hModule)
{
    HRSRC hRes = FindResource(hModule, MAKEINTRESOURCE(metaID), "emf");
    if (!hRes)
        return false;

    HGLOBAL hMeta = LoadResource(hModule, hRes);
    if (!hMeta)
        return false;

    BYTE* pData = (BYTE*)GlobalLock(hMeta);
    if (!pData)
        return false;

    m_hMetaFile = SetEnhMetaFileBits(SizeofResource(hModule, hRes), pData);
    
    BOOL bRes = ModifyStyle(0, SS_ENHMETAFILE)        // allow metafiles
        && SetEnhMetaFile(m_hMetaFile) != NULL;    // set metafile

    GlobalUnlock (hMeta);

    return bRes;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI am looking for a simple MFC Control with Draw Area Pin
p2002ad25-May-05 6:31
p2002ad25-May-05 6:31 

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.