home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / metafile / cmetafil.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.4 KB  |  122 lines

  1. /*
  2.  *  This program demonstrates the use of the function CloseMetaFile.
  3.  *  This function closes the metafile DC and creates a metafile handle
  4.  *  that can be used to play the metafile using PlayMetaFile.
  5.  *
  6.  */
  7.  
  8. #include <windows.h>
  9.  
  10. long    FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  11.  
  12. static char    szAppName[] = "cmetafil";
  13. static char    szFuncName[] = "CloseMetaFile";
  14.  
  15. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  16. HANDLE hInstance, hPrevInstance;
  17. LPSTR  lpszCmdLine;
  18. int    cmdShow;
  19. {
  20.   HWND     hWnd;
  21.   WNDCLASS rClass;
  22.   MSG      msg;
  23.  
  24.   if (!hPrevInstance)
  25.   {
  26.     rClass.style         = CS_HREDRAW | CS_VREDRAW;
  27.     rClass.lpfnWndProc   = WndProc;
  28.     rClass.cbClsExtra    = 0;
  29.     rClass.cbWndExtra    = 0;
  30.     rClass.hInstance     = hInstance;
  31.     rClass.hIcon         = LoadIcon (hInstance, IDI_APPLICATION);
  32.     rClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  33.     rClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  34.     rClass.lpszMenuName  = NULL;
  35.     rClass.lpszClassName = szAppName;
  36.  
  37.     if (!RegisterClass (&rClass))
  38.       return TRUE;
  39.   }
  40.  
  41. hWnd = CreateWindow (szAppName,            /* window class name       */
  42. szFuncName,                 /* window caption          */
  43. WS_OVERLAPPEDWINDOW,        /* window style            */
  44. CW_USEDEFAULT,              /* initial x position      */
  45. 0,                          /* initial y position      */
  46. CW_USEDEFAULT,              /* initial x size          */
  47. 0,                          /* initial y size          */
  48. NULL,                       /* parent window handle    */
  49. NULL,                       /* window menu handle      */
  50. hInstance,                  /* program instance handle */
  51.   NULL);                      /* create parameters       */
  52.  
  53.   ShowWindow (hWnd, cmdShow);
  54.   UpdateWindow (hWnd);
  55.  
  56.   while (GetMessage (&msg, NULL, 0, 0))
  57.   {
  58.     TranslateMessage (&msg);
  59.     DispatchMessage (&msg);
  60.   }
  61.   return (msg.wParam);
  62. }
  63.  
  64.  
  65. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  66. HWND     hWnd;
  67. unsigned    iMessage;
  68. WORD     wParam;
  69. LONG     lParam;
  70. {
  71.   HANDLE hMF;
  72.   HDC    hDCMeta;
  73.   HDC    hDC;
  74.   HMENU  hMenu;
  75.   BOOL   bPlayed;
  76.  
  77.   switch (iMessage)
  78.   {
  79.   case WM_CREATE:
  80.     hMenu = CreateMenu ();
  81.     ChangeMenu (hMenu, NULL, (LPSTR)"Play", 100, MF_APPEND);
  82.     SetMenu (hWnd, hMenu);
  83.     break;
  84.   case WM_INITMENU:
  85.     InvalidateRect (hWnd, NULL, TRUE);
  86.     break;
  87.   case WM_COMMAND:
  88.     if (wParam == 100)
  89.     {
  90.       hDC = GetDC (hWnd);
  91.       hDCMeta = CreateMetaFile ((LPSTR)"CMETAFIL.MET");
  92.       Rectangle (hDCMeta, 10, 10, 600, 200);
  93.       Arc (hDCMeta, 10, 10, 600, 200, 10, 15, 600, 150);
  94.       MessageBox (GetFocus(), (LPSTR)"Creating handle to metafile",
  95.           (LPSTR) szFuncName, MB_OK);
  96.       hMF = CloseMetaFile (hDCMeta);
  97.       if (hMF != NULL)
  98.       {
  99.     MessageBox (GetFocus(), (LPSTR)"Metafile handle created",
  100.         (LPSTR) szFuncName, MB_OK);
  101.     bPlayed = PlayMetaFile (hDC, hMF);
  102.     MessageBox (GetFocus(), (LPSTR)"This is the metafile",
  103.         (LPSTR) szFuncName, MB_OK);
  104.       }
  105.       else
  106.     MessageBox (GetFocus(), (LPSTR)"Metafile handle not created",
  107.         (LPSTR) szFuncName, MB_OK);
  108.       DeleteMetaFile (hMF);
  109.       ReleaseDC (hWnd, hDC);
  110.     }
  111.     break;
  112.   case WM_DESTROY:
  113.     PostQuitMessage (0);
  114.     break;
  115.   default:
  116.     return DefWindowProc (hWnd, iMessage, wParam, lParam);
  117.   }
  118.   return (0L);
  119. }
  120.  
  121.  
  122.