Windows Metafile(WMF)

Windows metafile is a graphical file format to capture GDI section in a file storage. WMF is the extension for this image format files. The file can be used to retrive the same GDI drwaing later in the window. Windows provide Win32 API calls to save and retrieve the GDI drawings. Below are the APIs and their utilities.

Metafile APIs

The following functions are used with enhanced-format metafiles.

Function Description
CloseEnhMetaFile Closes an enhanced-metafile device context.
CopyEnhMetaFile Copies the contents of an enhanced-format metafile to a specified file.
CreateEnhMetaFile Creates a device context for an enhanced-format metafile.
DeleteEnhMetaFile Deletes an enhanced-format metafile or an enhanced-format metafile handle.
EnhMetaFileProc An application-defined callback function used with the EnumEnhMetaFile function.
EnumEnhMetaFile Enumerates the records within an enhanced-format metafile.
GdiComment Copies a comment from a buffer into a specified enhanced-format metafile.
GetEnhMetaFile Creates a handle that identifies the enhanced-format metafile stored in the specified file.
GetEnhMetaFileBits Retrieves the contents of the specified enhanced-format metafile and copies them into a buffer.
GetEnhMetaFileDescription Retrieves an optional text description from an enhanced-format metafile and copies the string to the specified buffer.
GetEnhMetaFileHeader Retrieves the record containing the header for the specified enhanced-format metafile.
GetEnhMetaFilePaletteEntries Retrieves optional palette entries from the specified enhanced metafile.
GetMetaFile GetMetaFile is no longer available for use as of Windows 2000. Instead, use GetEnhMetaFile.
GetWinMetaFileBits Converts the enhanced-format records from a metafile into Windows-format records.
PlayEnhMetaFile Displays the picture stored in the specified enhanced-format metafile.
PlayEnhMetaFileRecord Plays an enhanced-metafile record by executing the graphics device interface (GDI) functions identified by the record.
SetEnhMetaFileBits Creates a memory-based enhanced-format metafile from the specified data.
SetWinMetaFileBits Converts a metafile from the older Windows format to the new enhanced format.

Save Windows Metafile

CreateEnhMetaFile() is the function to create a Windows Meta File and meta device context from existing device context. This meta device context can be used to perform different GDI drawings. Then the device context can be closed with CloseEnhMetaFile(). This will finalize the file context to disk.

void CMetafileDlg::OnSaveWPF() 
  CDC* WindowDC = GetDC();
  RECT rect;
  int iWidthMM = GetDeviceCaps(WindowDC->m_hDC, HORZSIZE); 
  int iHeightMM = GetDeviceCaps(WindowDC->m_hDC, VERTSIZE); 
  int iWidthPels = GetDeviceCaps(WindowDC->m_hDC, HORZRES); 
  int iHeightPels = GetDeviceCaps(WindowDC->m_hDC, VERTRES); 
  // Retrieve the coordinates of the client  
  // rectangle, in pixels.  

  // Convert client coordinates to .01-mm units.  
  // Use iWidthMM, iWidthPels, iHeightMM, and  
  // iHeightPels to determine the number of  
  // .01-millimeter units per pixel in the x-  
  //  and y-directions.  
  rect.left = (rect.left * iWidthMM * 100)/iWidthPels; = ( * iHeightMM * 100)/iHeightPels; 
  rect.right = (rect.right * iWidthMM * 100)/iWidthPels; 
  rect.bottom = (rect.bottom * iHeightMM * 100)/iHeightPels; 
  HDC hdcMeta = CreateEnhMetaFile(WindowDC->m_hDC, 
          &rect, (LPSTR)"Dialog Meta"); 
  HPEN hPen = CreatePen(0, 2, RGB(255,0,0));
  SelectObject(hdcMeta, hPen);
  Rectangle(hdcMeta, 10,10,200,200);
  // Release the reference device context.  

Restore Windows Metafile

Metafile restore from disk to device context is the reverse process. GetEnhMetaFile() takes the file path and returns the handle to the file. This metafile handle can be used to call PlayEnhMetaFile() to re-play the metafile context to a device context. We are restoring the metafile to our dialog device context. The drawing is now visible in the dialog window. Finally the metafile handle can be closed with DeleteEnhMetaFile() call.

void CMetafileDlg::OnRestoreWPF() 
  RECT rct;
  HENHMETAFILE hemf = GetEnhMetaFileA("Meta.wmf"); 
  // Retrieve a handle to a window device context.  
  CDC* hDC = GetDC(); 
  // Retrieve the client rectangle dimensions.  
  GetClientRect( &rct); 
  // Draw the picture.  
  if(PlayEnhMetaFile(hDC->m_hDC, hemf, &rct) == 0)
  // Release the metafile handle.  
  // Release the window DC.  

Windows Metafile Demo

Windows Metafile Demo

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...