74.2.2 Enhanced Metafile Operations

You create an enhanced metafile by calling the CreateEnhMetaFile function and supplying the appropriate arguments. Win32 uses these arguments to maintain the picture dimensions, to determine whether the metafile should be stored on disk or in memory, and so on.

In order to maintain picture dimensions across devices, Win32 requires the resolution of the device upon which the picture first appeared. This device is referred to as the reference device. And the device context associated with this device is referred to as the reference DC. When you call the CreateEnhMetaFile function, you must supply a handle that identifies this DC. You can obtain a reference DC handle by calling the GetDC or CreateDC functions.

Most applications store pictures permanently and therefore create an enhanced metafile that is stored on disk; however, there are some instances when this is not necessary. For example, a word-processing application that provides chart-drawing capabilities could store a user-defined chart in memory as an enhanced metafile and then copy the enhanced metafile bits from memory into the user's document file. If your application requires a metafile that is stored permanently on disk, you should supply the filename when you call the CreateEnhMetaFile function; if you do not supply a filename, Win32 will automatically treat the metafile as a temporary file and store it in memory.

You can add an optional text description to a metafile which contains information about the picture, it's author, when it was created and so on. And, your application can display these strings in the File Open dialog—providing the user with information about metafile contents that will help them select the appropriate file. If your application includes the text description, it should supply a pointer to the string when it calls the CreateEnhMetaFile function.

When the CreateEnhMetaFile function executes successfully, it returns a handle that identifies a special metafile device-context. A metafile DC is unique in that it is associated with a file rather than an output device. When Win32 processes a GDI function that received a handle to a metafile DC, it converts the GDI function into and enhanced-metafile record and appends the record onto the end of the enhanced metafile.

Once a picture is complete and the last record has been appended onto the enhanced metafile, you should close it by calling the CloseEnhMetaFile function. This function closes and deletes the special metafile DC and returns a handle identifying the enhanced metafile. You can use this handle to accomplish the tasks in the following list:

Create copies of the enhanced metafile

Enumerate, edit, or copy the individual records in the enhanced metafile

Retrieve the optional description that is stored in the enhanced metafile

Retrieve a copy of the enhanced-metafile header

Retrieve a binary version of the enhanced metafile

Enumerate the colors in the optional palette

Display the picture stored in the enhanced metafile

Some applications create temporary backup (or duplicate) copies of a file before the user is able to begin altering the original. You can create a backup copy of an enhanced metafile by calling the CopyEnhMetaFile function, supplying a handle that identifies the enhanced metafile, and supplying a pointer to a string of text that specifies the name of the new file.

Most drawing, illustration, and CAD applications require a means of editing an existing picture that is stored in an enhanced metafile. Although editing an enhanced metafile is a complex task, you can use the EnumEnhMetaFile function in combination with other Win32 functions to provided this capability in your application. The EnumEnhMetaFile function and its associated callback function allow you to process the individual records in an existing enhanced metafile.

Some applications will display the text description of an enhanced metafile with the corresponding filename in the File Open dialog. An application can determine whether this string exists in an enhanced metafile by retrieving the metafile header and examining one of its members. The metafile header is retrieved by calling the GetEnhMetaFileHeader function. If the string exists, the application can retrieve it by calling the GetEnhMetaFileDescription function.

Some applications will create a copy of a metafile by calling the GetEnhMetaFileBits function; however, before creating this copy, the application must specify the size of the file. To retrieve the size, the application can call the GetEnhMetaFileHeader and examine the appropriate member.

In order to guarantee consistent colors when a picture is displayed on various devices, an application can call the CreatePalette function and store a logical palette in an enhanced metafile. Other applications, which display the picture stored in the enhanced metafile, can retrieve this palette and call the RealizePalette function prior to playing the metafile. To determine whether a palette is stored in an enhanced metafile, an application can retrieve the metafile header and examine the appropriate member. If a palette exists, the application can call the GetEnhMetaFilePaletteEntries function to retrieve the logical palette.