This section contains sample code that demonstrates the creation of an enhanced metafile that is stored on disk using a filename that is specified by the application's user.
The example uses a device context for the application's window as the reference DC. (Windows will store the resolution data for this device in the enhanced metafile's header.) The application obtained a handle identifying this DC by calling the GetDC function.
The example uses the dimensions of the application's client area to define the dimensions of the picture frame. Using the rectangle dimensions returned by the GetClientRect function, the application converted the device-units to .01 mm units and passed the converted values to the CreateEnhMetaFile function.
The example displays a Save As common dialog which lets the user specify the name of the new enhanced metafile. The three-character “.emf” extension is appended to this filename and passed to the CreateEnhMetaFile function.
The example also embeds a text description of the picture in the enhanced metafile header. In the example, this description is specified as a resource in the string table of the application's resource file. However, in a working application, this string would be retrieved from a custom control in a common dialog or from a separate dialog that is displayed solely for this purpose.
The following code demonstrates the creation of the enhanced metafile:
/* Obtain a handle to a reference DC. */
hdcRef = GetDC(hWnd);
/* Determine the picture-frame dimensions. In */
/* this case, these dimensions are identical */
/* to the dimensions of the app's client area.*/
/* iWidthMM is the display width in MM. */
/* iHeightMM is the display height in MM. */
/* iWidthPels is the display width in pixels. */
/* iHeightPels is the display height in pixels*/
iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE);
iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE);
iWidthPels = GetDeviceCaps(hdcRef, HORZRES);
iHeightPels = GetDeviceCaps(hdcRef, VERTRES);
/* */
/* Use iWidthMM, iWidthPels, iHeightMM, and */
/* iHeightPels to determine the number of .01 */
/* millimeter units per pixel in the x- and */
/* the y-direction. */
/* */
iMMPerPelX = (iWidthMM * 100)/iWidthPels;
iMMPerPelY = (iHeightMM * 100)/iHeightPels;
/* */
/* Retrieve the coordinates of the client */
/* rectangle in pixels. */
/* */
GetClientRect(hWnd, &rect);
/* Convert client coordinates to .01mm units. */
rect.left = rect.left * iMMPerPelX;
rect.top = rect.top * iMMPerPelY;
rect.right = rect.right * iMMPerPelX;
rect.bottom = rect.bottom * iMMPerPelY;
/* Load the filename filter from the string table. */
LoadString(hInst, IDS_FILTERSTRING,
(LPSTR)szFilter, sizeof(szFilter));
/* */
/* Replace the '%' separators that are embedded */
/* between the strings in the stringtable entry */
/* embedded between strings in the string-table */
/* entry with '\0'. */
/* */
for (i=0; szFilter[i]!='\0'; i++)
if (szFilter[i] == '%')
szFilter[i] = '\0';
/* Load the dialog-title string from the table. */
LoadString(hInst, IDS_TITLESTRING,
(LPSTR)szTitle, sizeof(szTitle));
/* Set all structure members to zero. */
memset(&Ofn, 0, sizeof(OPENFILENAME));
/* Initialize the OPENFILENAME members. */
szFile[0] = '\0';
Ofn.lStructSize = sizeof(OPENFILENAME);
Ofn.hwndOwner = hWnd;
Ofn.lpstrFilter = szFilter;
Ofn.lpstrFile= szFile;
Ofn.nMaxFile = sizeof(szFile);
Ofn.lpstrFileTitle = szFileTitle;
Ofn.nMaxFileTitle = sizeof(szFileTitle);
Ofn.lpstrInitialDir = (LPSTR)NULL;
Ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT;
Ofn.lpstrTitle = szTitle;
/**/
/* Display the filename common-dialog. The filename*/
/* specified by the user will be passed to the*/
/* CreateMetaFile function and used to store the*/
/* metafile on disk.*/
/**/
GetSaveFileName(&Ofn);
/* Load the description from the string table. */
LoadString(hInst, IDS_DESCRIPTIONSTRING,
(LPSTR)szFilter, sizeof(szFilter));
/*
/* Replace the '%' string-separators that are */
/* embedded between strings in the string-table */
/* entry with '\0'. */
/* */
for (i=0; szDescription[i]!='\0'; i++)
if (szDescription[i] == '%')
szDescription[i] = '\0';
/* Create the MetaFile DC. */
hdcMeta = CreateEnhMetaFile(hdcRef,
(LPTSTR) Ofn.lpstrFile,
&rect, "SDK Sample App\0Front View\0\0");
if (!hdcMeta)
errhandler("CreateEnhMetaFile", hWnd);
/* Release the reference DC. */
ReleaseDC(hWnd, hdcRef);