home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n8.zip / GASH.ZIP / METAFILE / MET3.C < prev    next >
C/C++ Source or Header  |  1993-08-14  |  2KB  |  82 lines

  1. /*
  2.  
  3. MET3.C - Writes a metafile with an optional placeable header.
  4.          (C) 1993, Dennis CHUAH
  5.  
  6. */
  7.  
  8.  
  9. #define STRICT
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <metafile.h>
  13.  
  14.  
  15. HMETAFILE WINAPI CopyMetaFileBetter (HMETAFILE hmfSrc, LPCSTR lpszFile,
  16.                                      METAFILEHEADER far *lpMh)
  17.   {HMETAFILE hMetafile;
  18.    METAHEADER far *lpHeader;
  19.    HGLOBAL handle;
  20.    long filesize;
  21.    HFILE hFile;
  22.    long wrote;
  23.  
  24.    if (hmfSrc == NULL) return NULL;
  25.  
  26.    /* If specifying a memory metafile destination copy to a
  27.       non-placeable metafile -- as memory metafiles cannot have
  28.       a placeable header.  If the pointer to the METAFILEHEADER
  29.       or the METAFILEHEADER is incorrect -- don't copy to a
  30.       placeable metafile */
  31.    if (lpszFile == NULL ||
  32.        IsBadHugeReadPtr (lpMh, sizeof (METAFILEHEADER) ||
  33.        lpMh->key != 0x9AC6CDD7L))
  34.      {return CopyMetaFile (hmfSrc, lpszFile);
  35.      }
  36.  
  37.    /* Get the metafile data */
  38.    hMetafile = CopyMetaFile (hmfSrc, NULL);
  39.    if (hMetafile == NULL) return NULL;
  40.    handle = GetMetaFileBits (hMetafile);
  41.    if (handle == NULL) return NULL;
  42.    lpHeader = GlobalLock (handle);
  43.  
  44.    /* Get the size of the metafile.  Multiply by two as mtSize is in
  45.       words */
  46.    filesize = lpHeader->mtSize * 2;
  47.  
  48.    /* Open a disk file to place the metafile into */
  49.    hFile = _lcreat (lpszFile, 0);
  50.    if (hFile == HFILE_ERROR)
  51.      {GlobalUnlock (handle);
  52.       GlobalFree (handle);
  53.       return NULL;
  54.      }
  55.  
  56.    /* Write placeable header */
  57.    wrote = _hwrite (hFile, lpMh, sizeof (METAFILEHEADER));
  58.    if (wrote == -1 || wrote < sizeof (METAFILEHEADER))
  59.      {_lclose (hFile);
  60.       GlobalUnlock (handle);
  61.       GlobalFree (handle);
  62.       return NULL;
  63.      }
  64.  
  65.    /* Write the rest of the metafile */
  66.    wrote = _hwrite (hFile, lpHeader, filesize);
  67.    if (wrote == -1 || wrote < filesize)
  68.      {_lclose (hFile);
  69.       GlobalUnlock (handle);
  70.       GlobalFree (handle);
  71.       return NULL;
  72.      }
  73.  
  74.    _lclose (hFile);
  75.  
  76.    /* Return a handle to the new metafile */
  77.    GlobalUnlock (handle);
  78.    return (HMETAFILE) SetMetaFileBitsBetter ((HMETAFILE) handle);
  79.   } // end CopyMetaFileBetter
  80.  
  81.  
  82.