Click here to Skip to main content
Click here to Skip to main content
Go to top

Using a CStatic control to display an EMF image

, 31 Aug 2002
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Holger Kloos

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 1 Sep 2002
Article Copyright 2002 by Holger Kloos
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid