A metafile for the Windows operating system consists of a collection of graphics device interface (GDI) functions that describe an image. Because metafiles take up less space and are more device- independent than bitmaps, they provide convenient storage for images that appear repeatedly in an application or need to be moved from one application to another. To generate a metafile, a Windows application creates a special device context that sends GDI commands to a file or memory for storage. The application can later play back the metafile and display the image. During playback, Windows breaks the metafile down into records and identifies each object with an index to a handle table.
When a META_DELETEOBJECT record is encountered during playback, the associated object is deleted from the handle table. The entry is then reused by the next object that the metafile creates. To ensure compatibility, an application that explicitly manipulates records or builds its own metafile should manage the handle table in the same way. For more information on the format of the handle table, see the HANDLETABLE structure. In some cases, there are two variants of a metafile record, one representing the record created by Windows versions before 3.0 and the second representing the record created by Windows versions 3.0 and later. Windows versions 3.0 and later play all metafile versions but store only 3.0 and later versions. Windows versions earlier than 3.0 do not play metafiles recorded by Windows versions 3.0 and later. A metafile consists of two parts: a header and a list of records. The header and records are described in the remainder of this topic. For a list of function-specific records, see Metafile Records.
Metafile Header
The metafile header contains a description of the size of the metafile and the number of drawing objects it uses. The drawing objects can be pens, brushes, bitmaps, or fonts. The metafile header has the following form:
typedef struct tagMETAHEADER { WORD mtType; WORD mtHeaderSize; WORD mtVersion; DWORD mtSize; WORD mtNoObjects; DWORD mtMaxRecord; WORD mtNoParameters; } METAHEADER;
Following are the members in the metafile header:
mtType Specifies whether the metafile is stored in memory or recorded in a
file. This member has one of the following values:
Value Meaning
0 Metafile is in memory.
1 Metafile is in a file.
mtHeaderSize Specifies the size, in words, of the metafile header.
mtVersion Specifies the Windows version number. The version number for Windows
version 3.0 and later is 0x300.
mtSize Specifies the size, in words, of the file.
mtNoObjects Specifies the maximum number of objects that can exist in the
metafile at the same time.
mtMaxRecord Specifies the size, in words, of the largest record in the metafile.
mtNoParameters Not used.
Typical Metafile Record
The graphics device interface stores most of the GDI functions that an
application can use to create metafiles in typical records. A typical metafile
record has the following form:
struct {
DWORD rdSize;
WORD rdFunction;
WORD rdParm[];
}
Following are the members in a typical metafile record:
rdSize Specifies the size, in words, of the record.
rdFunction Specifies the function number. This value may be the number of any
function in the
table at the end of this section.
rdParm Identifies an array of words containing the function parameters (listed
in
the reverse
order in which they are passed to the function).
Following are the GDI functions found in typical records, along with their
hexadecimal values:
GDI function Value
Arc 0x0817
Chord 0x0830
Ellipse 0x0418
ExcludeClipRect 0x0415
FloodFill 0x0419
IntersectClipRect 0x0416
LineTo 0x0213
MoveTo 0x0214
OffsetClipRgn 0x0220
OffsetViewportOrg 0x0211
OffsetWindowOrg 0x020F
PatBlt 0x061D
Pie 0x081A
RealizePalette (3.0 and later) 0x0035
Rectangle 0x041B
ResizePalette (3.0 and later) 0x0139
RestoreDC 0x0127
RoundRect 0x061C
SaveDC 0x001E
ScaleViewportExt 0x0412
ScaleWindowExt 0x0400
SetBkColor 0x0201
SetBkMode 0x0102
SetMapMode 0x0103
SetMapperFlags 0x0231
SetPixel 0x041F
SetPolyFillMode 0x0106
SetROP2 0x0104
SetStretchBltMode 0x0107
SetTextAlign 0x012E
SetTextCharacterExtra 0x0108
SetTextColor 0x0209
SetTextJustification 0x020A
SetViewportExt 0x020E
SetViewportOrg 0x020D
SetWindowExt 0x020C
SetWindowOrg 0x020B
Placeable Windows Metafiles
A placeable Windows metafile is a standard Windows metafile that has an
additional 22-byte header. The header contains information about the aspect
ratio and original size of the metafile, permitting applications to display the
metafile in its intended form. The header for a placeable Windows metafile has
the following form:
typedef struct {
DWORD key;
HANDLE hmf;
RECT bbox;
WORD inch;
DWORD reserved;
WORD checksum;
} METAFILEHEADER;
Following are the members of a placeable metafile header:
key Specifies the binary key that uniquely identifies this file type. This
member must be set to 0x9AC6CDD7L. hmf Unused; must be zero.
bbox Specifies the coordinates of the smallest rectangle that encloses the
picture.
The coordinates are in metafile units as defined by the inch member.
inch Specifies the number of metafile units to the inch. To avoid numeric
overflow, this value
should be less than 1440. Most applications use 576 or 1000.
reserved Unused; must be zero.
checksum Specifies the checksum. It is the sum (using the XOR operator) of the
first 10 words of
the header.
The actual content of the Windows metafile immediately follows the header.
The format for this content is identical to that for standard Windows metafiles.
For some applications, a placeable Windows metafile must not exceed 64K.
Note: Placeable Windows metafiles are not compatible with the GetMetaFile
function. Applications that intend to use the metafile functions to read and
play placeable Windows metafiles must read the file by using an input function
(such as _lread), strip the 22-byte header, and create a standard Windows
metafile by using the remaining bytes and the SetMetaFileBits function.
Guidelines for Windows Metafiles
To ensure that metafiles can be transported between different computers and
applications, any application that creates a metafile should make sure the
metafile is device-independent and sizable. The following guidelines ensure that
every metafile can be accepted and manipulated by other applications:
Set a mapping mode as one of the first records. Many applications, including
OLE applications, only accept metafiles that are in MM_ANISOTROPIC mode.
Call the SetWindowOrg and SetWindowExt functions. Do not call the
SetViewportExt or SetViewportOrg functions if the user will be able to resize or
change the dimensions of the object.
Use the MFCOMMENT printer escape to add comments to the metafile.
Rely primarily on the functions listed in Typical Metafile Record. Observe
the following limitations on the functions you use:
Do not use functions that retrieve data (for example, GetActiveWindow or
EnumFontFamilies).
Do not use any of the region functions (because they are device dependent).
Use StretchBlt or StretchDIB instead of BitBlt.
Sample of Metafile Program Output
This section describes a sample program and the metafile that it creates. The
sample program creates a small metafile that draws a purple rectangle with a
green border and writes the words «Hello People» in the rectangle.
MakeAMetaFile(hDC)
HDC hDC;
{
HPEN hMetaGreenPen;
HBRUSH hMetaVioletBrush;
HDC hDCMeta;
HANDLE hMeta;
/* Create the metafile with output going to the disk. */
hDCMeta = CreateMetaFile( (LPSTR) «sample.met»);
hMetaGreenPen = CreatePen(0, 0, (DWORD) 0x0000FF00);
SelectObject(hDCMeta, hMetaGreenPen);
hMetaVioletBrush = CreateSolidBrush((DWORD) 0x00FF00FF);
SelectObject(hDCMeta, hMetaVioletBrush);
Rectangle(hDCMeta, 0, 0, 150, 70);
TextOut(hDCMeta, 10, 10, (LPSTR) «Hello People»,
12);
/* We are done with the metafile. */
hMeta = CloseMetaFile(hDCMeta);
/* Play the metafile that we just created. */
PlayMetaFile(hDC, hMeta);
}
The resulting metafile, SAMPLE.MET, consists of a metafile header and six
records.
It has the following
binary form:
0001 mtType… disk metafile
0009 mtSize…
0300 mtVersion
0000 0036 mtSize
0002 mtNoObjects
0000 000C mtMaxRecord
0000 mtNoParameters
0000 0008 rdSize
02FA rdFunction
(CreatePenIndirect function)
0000 0000 0000 0000 FF00 rdParm (LOGPEN structure defining pen)
0000 0004 rdSize
012D rdFunction (SelectObject)
0000 rdParm (index to object
#0… the above pen)
0000 0007 rdSize
02FC rdFunction
(CreateBrushIndirect)
0000 00FF 00FF 0000 rdParm (LOGBRUSH structure defining the brush)
0000 0004 rdSize
012D rdFunction (SelectObject)
0001 rdParm (index to object
#1… the brush)
0000 0007 rdSize
041B rdFunction (Rectangle)
0046 0096 0000 0000 rdParm (parameters sent to Rectangle…
in reverse order)
0000 000C rdSize
0521 rdFunction (TextOut)
rdParm
000C count
string
48 65 6C 6C 6F 20 50 65 6F 70 6C 65 «Hello People»
000A
y-value
000A
x-value