home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / PMCARD.ZIP / METAFILE.C < prev    next >
C/C++ Source or Header  |  1990-07-11  |  5KB  |  149 lines

  1. /*--------------------------------------------------------------
  2.    METAFILE.C -- Demonstrates GPI metafile creation and playing
  3.                  (c) Ziff Communications Co, 1990
  4.                  PC Magazine * Charles Petzold, 7/90
  5.   --------------------------------------------------------------*/
  6.  
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #include <os2.h>
  10. #include <stdio.h>
  11.  
  12. MRESULT EXPENTRY ClientWndProc  (HWND, USHORT, MPARAM, MPARAM) ;
  13. HMF              CreateMetaFile (void) ;
  14.  
  15. HAB  hab ;
  16.  
  17. int main (void)
  18.      {
  19.      static CHAR  szClientClass[] = "Metafile" ;
  20.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU |
  21.                                  FCF_SIZEBORDER    | FCF_MINMAX  |
  22.                                  FCF_SHELLPOSITION | FCF_TASKLIST ;
  23.      HMQ          hmq ;
  24.      HWND         hwndFrame, hwndClient ;
  25.      QMSG         qmsg ;
  26.  
  27.      hab = WinInitialize (0) ;
  28.      hmq = WinCreateMsgQueue (hab, 0) ;
  29.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  30.  
  31.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  32.                                      &flFrameFlags, szClientClass, NULL,
  33.                                      0L, 0, 0, &hwndClient) ;
  34.  
  35.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  36.           WinDispatchMsg (hab, &qmsg) ;
  37.  
  38.      WinDestroyWindow (hwndFrame) ;
  39.      WinDestroyMsgQueue (hmq) ;
  40.      WinTerminate (hab) ;
  41.      return 0 ;
  42.      }
  43.  
  44. #include "metarect.c"
  45.  
  46. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  47.      {
  48.      static HMF   hmf ;
  49.      static RECTL rclMetaFile, rclClient ;
  50.      HPS          hps ;
  51.      MATRIXLF     matlf ;
  52.  
  53.      switch (msg)
  54.           {
  55.           case WM_CREATE:
  56.                hmf = CreateMetaFile () ;
  57.                QueryMetaFileBoundaryRect (hab, hmf, &rclMetaFile) ;
  58.                return 0 ;
  59.  
  60.           case WM_SIZE:
  61.                WinQueryWindowRect (hwnd, &rclClient) ;
  62.                return 0 ;
  63.  
  64.           case WM_PAINT:
  65.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  66.                GpiErase (hps) ;
  67.  
  68.                          // Translation transform
  69.  
  70.                matlf.fxM11 = MAKEFIXED (1, 0) ;
  71.                matlf.fxM12 = 0 ;
  72.                matlf.lM13  = 0 ;
  73.  
  74.                matlf.fxM21 = 0 ;
  75.                matlf.fxM22 = MAKEFIXED (1, 0) ;
  76.                matlf.lM23  = 0 ;
  77.  
  78.                matlf.lM31  = -rclMetaFile.xLeft ;
  79.                matlf.lM32  = -rclMetaFile.yBottom ;
  80.                matlf.lM33  = 1 ;
  81.  
  82.                GpiSetDefaultViewMatrix (hps, 9L, &matlf, TRANSFORM_REPLACE) ;
  83.  
  84.                          // Scaling transform
  85.  
  86.                matlf.fxM11 = 65536L * (rclClient.xRight - 1) /
  87.                              (rclMetaFile.xRight - rclMetaFile.xLeft) ;
  88.  
  89.                matlf.fxM22 = 65536L * (rclClient.yTop - 1) /
  90.                              (rclMetaFile.yTop - rclMetaFile.yBottom) ;
  91.  
  92.                matlf.lM31  = 0 ;
  93.                matlf.lM32  = 0 ;
  94.  
  95.                GpiSetDefaultViewMatrix (hps, 9L, &matlf, TRANSFORM_ADD) ;
  96.  
  97.                          // Play the metafile using this composite transform
  98.  
  99.                GpiPlayMetaFile (hps, hmf, 0L, NULL, NULL, 0L, NULL) ;
  100.  
  101.                WinEndPaint (hps) ;
  102.                return 0 ;
  103.  
  104.           case WM_DESTROY:
  105.                GpiDeleteMetaFile (hmf) ;
  106.                return 0 ;
  107.           }
  108.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  109.      }
  110.  
  111. HMF CreateMetaFile (void)
  112.      {
  113.      static DEVOPENSTRUC dop = { NULL, "DISPLAY" } ;
  114.      static POINTL       aptl[] = { -50,-50, -50,50, 0,100,  50,50,
  115.                                     -50,-50, 50,-50, 50,50, -50,50, 50,-50 } ;
  116.      HDC                 hdcMetafile ;
  117.      HMF                 hmf ;
  118.      HPS                 hps ;
  119.      SIZEL               sizlPage ;
  120.  
  121.                // Open a metafile device context
  122.  
  123.      hdcMetafile = DevOpenDC (hab, OD_METAFILE, "*", 2L,
  124.                               (PDEVOPENDATA) &dop, NULL) ;
  125.  
  126.                // Create a presentation space associated with that DC
  127.  
  128.      sizlPage.cx = 100 ;
  129.      sizlPage.cy = 150 ;
  130.  
  131.      hps = GpiCreatePS (hab, hdcMetafile, &sizlPage,
  132.                         GPIT_MICRO | GPIA_ASSOC | PU_LOENGLISH) ;
  133.  
  134.                // Draw a house using eight lines
  135.  
  136.      GpiMove (hps, aptl) ;
  137.      GpiPolyLine (hps, 8L, aptl + 1) ;
  138.  
  139.                // Destroy the presentation space
  140.  
  141.      GpiDestroyPS (hps) ;
  142.  
  143.                // Close the device context to get the metafile handle
  144.  
  145.      hmf = DevCloseDC (hdcMetafile) ;
  146.  
  147.      return hmf ;
  148.      }
  149.