EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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; 
  rect.top = (rect.top * 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

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 21:45:49(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.