home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / METAFIL2.ZIP / METAFILE.C next >
C/C++ Source or Header  |  1990-06-14  |  3KB  |  109 lines

  1. /*--------------------------------------------------------------
  2.    METAFILE.C -- Demonstrates GPI metafile creation and playing
  3.                  (c) Ziff Communications Co, 1990
  4.                  PC Magazine * Charles Petzold, 6/90
  5.   --------------------------------------------------------------*/
  6.  
  7. #define INCL_GPI
  8. #include <os2.h>
  9.  
  10. MRESULT EXPENTRY ClientWndProc  (HWND, USHORT, MPARAM, MPARAM) ;
  11. HMF              CreateMetaFile (void) ;
  12.  
  13. HAB  hab ;
  14.  
  15. int main (void)
  16.      {
  17.      static CHAR  szClientClass[] = "Metafile" ;
  18.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU |
  19.                                  FCF_SIZEBORDER    | FCF_MINMAX  |
  20.                                  FCF_SHELLPOSITION | FCF_TASKLIST ;
  21.      HMQ          hmq ;
  22.      HWND         hwndFrame, hwndClient ;
  23.      QMSG         qmsg ;
  24.  
  25.      hab = WinInitialize (0) ;
  26.      hmq = WinCreateMsgQueue (hab, 0) ;
  27.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  28.  
  29.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  30.                                      &flFrameFlags, szClientClass, NULL,
  31.                                      0L, 0, 0, &hwndClient) ;
  32.  
  33.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  34.           WinDispatchMsg (hab, &qmsg) ;
  35.  
  36.      WinDestroyWindow (hwndFrame) ;
  37.      WinDestroyMsgQueue (hmq) ;
  38.      WinTerminate (hab) ;
  39.      return 0 ;
  40.      }
  41.  
  42. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  43.      {
  44.      static CHAR szFilename [] = "HOUSE.MET" ;
  45.      static HMF  hmf ;
  46.      HPS         hps ;
  47.  
  48.      switch (msg)
  49.           {
  50.           case WM_CREATE:
  51.                hmf = CreateMetaFile () ;
  52.                return 0 ;
  53.  
  54.           case WM_PAINT:
  55.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  56.                GpiErase (hps) ;
  57.  
  58.                GpiPlayMetaFile (hps, hmf, 0L, NULL, NULL, 0L, NULL) ;
  59.  
  60.                WinEndPaint (hps) ;
  61.                return 0 ;
  62.  
  63.           case WM_DESTROY:
  64.                DosDelete (szFilename, 0L) ;
  65.                GpiSaveMetaFile (hmf, szFilename) ;
  66.                return 0 ;
  67.           }
  68.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  69.      }
  70.  
  71. HMF CreateMetaFile (void)
  72.      {
  73.      static DEVOPENSTRUC dop = { NULL, "DISPLAY" } ;
  74.      static POINTL       aptl[] = { 0,0,  0,100,   50,150,  100,100,
  75.                                     0,0,  100,0,  100,100,    0,100,  100,0 } ;
  76.      HDC                 hdcMetafile ;
  77.      HMF                 hmf ;
  78.      HPS                 hps ;
  79.      SIZEL               sizlPage ;
  80.  
  81.                // Open a metafile device context
  82.  
  83.      hdcMetafile = DevOpenDC (hab, OD_METAFILE, "*", 2L,
  84.                               (PDEVOPENDATA) &dop, NULL) ;
  85.  
  86.                // Create a presentation space associated with that DC
  87.  
  88.      sizlPage.cx = 100 ;
  89.      sizlPage.cy = 150 ;
  90.  
  91.      hps = GpiCreatePS (hab, hdcMetafile, &sizlPage,
  92.                         GPIT_MICRO | GPIA_ASSOC | PU_LOENGLISH) ;
  93.  
  94.                // Draw a house using eight lines
  95.  
  96.      GpiMove (hps, aptl) ;
  97.      GpiPolyLine (hps, 8L, aptl + 1) ;
  98.  
  99.                // Destroy the presentation space
  100.  
  101.      GpiDestroyPS (hps) ;
  102.  
  103.                // Close the device context to get the metafile handle
  104.  
  105.      hmf = DevCloseDC (hdcMetafile) ;
  106.  
  107.      return hmf ;
  108.      }
  109.