打开增强型图元文件并显示其内容
本部分包含一个示例,演示应用程序如何打开存储在磁盘上的增强型图元文件,并在工作区中显示关联的图片。
该示例使用 “打开 通用”对话框使用户能够从现有文件列表中选择增强的图元文件。 然后,它将所选文件的名称传递给 GetEnhMetaFile 函数,该函数返回标识文件的句柄。 此句柄将传递给 PlayEnhMetaFile 函数,以便显示图片。
LoadString(hInst, IDS_FILTERSTRING,
(LPSTR)szFilter, sizeof(szFilter));
// Replace occurrences of '%' string separator
// with '\0'.
for (i=0; szFilter[i]!='\0'; i++)
{
if (szFilter[i] == '%')
szFilter[i] = '\0';
}
LoadString(hInst, IDS_DEFEXTSTRING,
(LPSTR)szDefExt, sizeof(szFilter));
// Use the OpenFilename common dialog box
// to obtain the desired filename.
szFile[0] = '\0';
OPENFILENAME Ofn;
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.
HENHMETAFILE hemf = GetEnhMetaFile(Ofn.lpstrFile);
// Retrieve a handle to a window device context.
HDC hDC = GetDC(hWnd);
// Retrieve the client rectangle dimensions.
GetClientRect(hWnd, &rct);
// Draw the picture.
PlayEnhMetaFile(hDC, hemf, &rct);
// Release the metafile handle.
DeleteEnhMetaFile(hemf);
// Release the window DC.
ReleaseDC(hWnd, hDC);