home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / METAFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.5 KB  |  109 lines

  1. /*----------------------------------------------
  2.    METAFILE.C -- Metafile Demonstration Program
  3.                  (c) Charles Petzold, 1996
  4.   ----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12.      {
  13.      static char szAppName [] = "Metafile" ;
  14.      HWND        hwnd ;
  15.      MSG         msg ;
  16.      WNDCLASSEX  wndclass ;
  17.  
  18.      wndclass.cbSize        = sizeof (wndclass) ;
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  30.  
  31.      RegisterClassEx (&wndclass) ;
  32.  
  33.      hwnd = CreateWindow (szAppName, "Metafile Demonstration",
  34.                           WS_OVERLAPPEDWINDOW,
  35.                           CW_USEDEFAULT, CW_USEDEFAULT,
  36.                           CW_USEDEFAULT, CW_USEDEFAULT,
  37.                           NULL, NULL, hInstance, NULL) ;
  38.  
  39.      ShowWindow (hwnd, iCmdShow) ;
  40.      UpdateWindow (hwnd) ;
  41.  
  42.      while (GetMessage (&msg, NULL, 0, 0))
  43.           {
  44.           TranslateMessage (&msg) ;
  45.           DispatchMessage (&msg) ;
  46.           }
  47.      return msg.wParam ;
  48.      }
  49.  
  50. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  51.      {
  52.      static HMETAFILE hmf ;
  53.      static int       cxClient, cyClient ;
  54.      HBRUSH           hBrush ;
  55.      HDC              hdc, hdcMeta ;
  56.      int              x, y ;
  57.      PAINTSTRUCT      ps ;
  58.  
  59.      switch (iMsg)
  60.           {
  61.           case WM_CREATE:
  62.                hdcMeta = CreateMetaFile (NULL) ;
  63.                hBrush  = CreateSolidBrush (RGB (0, 0, 255)) ;
  64.  
  65.                Rectangle (hdcMeta, 0, 0, 100, 100) ;
  66.  
  67.                MoveToEx (hdcMeta,   0,   0, NULL) ;
  68.                LineTo   (hdcMeta, 100, 100) ;
  69.                MoveToEx (hdcMeta,   0, 100, NULL) ;
  70.                LineTo   (hdcMeta, 100,   0) ;
  71.  
  72.                SelectObject (hdcMeta, hBrush) ;
  73.                Ellipse (hdcMeta, 20, 20, 80, 80) ;
  74.  
  75.                hmf = CloseMetaFile (hdcMeta) ;
  76.  
  77.                DeleteObject (hBrush) ;
  78.                return 0 ;
  79.  
  80.           case WM_SIZE:
  81.                cxClient = LOWORD (lParam) ;
  82.                cyClient = HIWORD (lParam) ;
  83.                return 0 ;
  84.  
  85.           case WM_PAINT:
  86.                hdc = BeginPaint (hwnd, &ps) ;
  87.  
  88.                SetMapMode (hdc, MM_ANISOTROPIC) ;
  89.                SetWindowExtEx (hdc, 1000, 1000, NULL) ;
  90.                SetViewportExtEx (hdc, cxClient, cyClient, NULL) ;
  91.  
  92.                for (x = 0 ; x < 10 ; x++)
  93.                     for (y = 0 ; y < 10 ; y++)
  94.                          {
  95.                          SetWindowOrgEx (hdc, -100 * x, -100 * y, NULL) ;
  96.                          PlayMetaFile (hdc, hmf) ;
  97.                          }
  98.  
  99.                EndPaint (hwnd, &ps) ;
  100.                return 0 ;
  101.  
  102.           case WM_DESTROY:
  103.                DeleteMetaFile (hmf) ;
  104.                PostQuitMessage (0) ;
  105.                return 0 ;
  106.           }
  107.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  108.      }
  109.