.wmf metafile format

a metafile for the microsoft 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.

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)
000c count
48 65 6c 6c 6f 20 50 65 6f 70 6c 65 "hello people"
000a y-value

000a x-value