How to Create & Play Enhanced Metafiles in Win32ID: Q145999
|
The Win32 SDK introduces a new type of metafile known as an enhanced
metafile. These new metafiles address developer's need for device
independence without requiring a separate code path, which was a
requirement of the older style metafiles.
This article and the accompanying sample (ENMETA.EXE) show you how to
properly create and play enhanced metafiles scaled or to original size. The
sample also supports the clipboard and reads and writes Aldus placeable
metafiles as well as regular 16-bit Windows metafiles.
The following file is available for download from the Microsoft Software Library:
~ Enmeta.exeFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services
HDC CreateEnhMetaFile( HDC hdcRef, LPCTSTR lpFilename,
CONST RECT *lpRect, LPCTSTR lpDescription );
Following are the parameters to this call:
App\0Description\0\0
App is the name of the application that created the metafile and
Description is a description of the image.
MetaPixelsX = MetaWidthMM * MetaPixels / (MetaMM * 100);
where MetaPixelsX = number of pixels on the X axis
MetaWidthMM = metafile width in 0.01mm units
MetaPixels = width in pixels of the reference device
MetaMM = width in millimeters of the reference device
A similar calculation can be used to determine the number of pixels in the
Y direction of the metafile device.
HDC MyCreateEnhMetaFile( LPTSTR szFileName, // Metafile filename
DWORD dwInchesX, // Width in inches
DWORD dwInchesY, // Height in inches
DWORD dwDPI ) // DPI (logical units)
{
RECT Rect = { 0, 0, 0, 0 };
TCHAR szDesc[] = "AppName\0Image Description\0\0";
HDC hMetaDC, hScreenDC;
float PixelsX, PixelsY, MMX, MMY;
// dwInchesX x dwInchesY in .01mm units
SetRect( &Rect, 0, 0, dwInchesX*2540, dwInchesY*2540 );
// Get a Reference DC
hScreenDC = GetDC( NULL );
// Get the physical characteristics of the reference DC
PixelsX = (float)GetDeviceCaps( hScreenDC, HORZRES );
PixelsY = (float)GetDeviceCaps( hScreenDC, VERTRES );
MMX = (float)GetDeviceCaps( hScreenDC, HORZSIZE );
MMY = (float)GetDeviceCaps( hScreenDC, VERTSIZE );
// Create the Metafile
hMetaDC = CreateEnhMetaFile(hScreenDC, szFileName, &Rect, szDesc);
// Release the reference DC
ReleaseDC( NULL, hScreenDC );
// Did you get a good metafile?
if( hMetaDC == NULL )
return NULL;
// Anisotropic mapping mode
SetMapMode( hMetaDC, MM_ANISOTROPIC );
// Set the Windows extent
SetWindowExtEx( hMetaDC, dwInchesX*dwDPI, dwInchesY*dwDPI, NULL );
// Set the viewport extent to reflect
// dwInchesX" x dwInchesY" in device units
SetViewportExtEx( hMetaDC,
(int)((float)dwInchesX*25.4f*PixelsX/MMX),
(int)((float)dwInchesY*25.4f*PixelsY/MMY),
NULL );
return hMetaDC;
}
Note that clipping is not performed by the window extents, viewport
extents, or the metafile device. It is possible to draw beyond the window
extents, which will map beyond the viewport extents. Those numbers provide
only a ratio of logical units to metafile device units. Further, it is
possible to have viewport extents that extend beyond the metafile surface.
The drawing is not clipped to the metafile surface. This mapping is
illustrated in the Sample2.emf sample enhanced metafile.
Additional query words: kbDSupport kbdsd kbGDI kbMetafile
Keywords : kbcode kbfile kbsample kbSDKWin32
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: June 24, 1999