
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)
&& SetEnhMetaFile(m_hMetaFile) != NULL;
GlobalUnlock (hMeta);
return bRes;
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.