This section contains sample code that demonstrates how an application can open an enhanced metafile (that was stored on disk) and display the associated picture in the client-area of its window.
The example uses the Open common dialog to let the user select an enhanced metafile from a list of existing files. It then passes the name of the selected file to the GetEnhMetaFile function which returns a handle identifying the file. This handle is passed to the PlayEnhMetaFile function in order to display the picture:
LoadString(hInst, IDS_FILTERSTRING,
(LPSTR)szFilter, sizeof(szFilter));
/* */
/* Replace occurences of '%' string seperator */
/* with '\0'. */
/* */
for (i=0; szFilter[i]!='\0'; i++)
if (szFilter[i] == '%')
szFilter[i] = '\0';
LoadString(hInst, IDS_DEFEXTSTRING,
(LPSTR)szDefExt, sizeof(szFilter));
/* Set all structure members to zero. */
memset(&Ofn, 0, sizeof(OPENFILENAME));
/* */
/* Use the OpenFilename common dialog to obtain */
/* the desired filename. */
/* */
szFile[0] = '\0';
Ofn.lStructSize = sizeof(OPENFILENAME);
Ofn.hwndOwner = hWnd;
Ofn.lpstrFilter = szFilter;
Ofn.lpstrCustomFilter = (LPSTR)NULL;
Ofn.nMaxCustFilter = 0L;
Ofn.nFilterIndex = 1L;
Ofn.lpstrFile = szFile;
Ofn.nMaxFile = sizeof(szFile);
Ofn.lpstrFileTitle = szFileTitle;
Ofn.nMaxFileTitle = sizeof(szFileTitle);
Ofn.lpstrInitialDir = (LPSTR) NULL;
Ofn.lpstrTitle = (LPSTR)NULL;
Ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
Ofn.nFileOffset = 0;
Ofn.nFileExtension = 0;
Ofn.lpstrDefExt = szDefExt;
GetOpenFileName(&Ofn);
/* Open the MetaFile. */
hemf = GetEnhMetaFile(Ofn.lpstrFile);
/* Retrieve a handle to a window DC. */
hDC = GetDC(hWnd);
/* Retrieve the client rectangle dimensions */
GetClientRect(hWnd, &rct);
/* Draw the picture. */
PlayEnhMetaFile(hDC, hemf, &rct);
/* Release the window DC. */
ReleaseDC(hWnd, hDC);
In order to edit an existing picture that is stored in an enhanced metafile, an application must perform the tasks in the following list:
1.Retrieve the coordinates of the object (line, arc, rectangle, ellipse, polygon, or irregular shape) that the user wishes to alter.
2.Convert these coordinates to logical (or world) units.
3.Call the EnumEnhMetaFile function and examine each metafile record:
4.Determine whether a given record corresponds to a GDI drawing function.
5.If it does, determine whether the coordinates (stored in the record) correspond to the line, arc, ellipse, etc. which intersects the coordinates specified by the user.
6.Upon finding the record that corresponds to the output which the user wishes to alter:
7.Erase the object on the screen that corresponds to the original record.
8.Delete the corresponding record from the metafile (saving a pointer to its location)
9.Let the user redraw or replace the object.
10.Convert the GDI functions (which were used to draw the new object) into one or more enhanced metafile records.
11.Store these records in the enhanced metafile.