DEMO6_1B.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:4k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo6_1b  ---  Play MetaFile                         */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo6_1b.h"
  7.   7 
  8.   8 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  9.   9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  10.  10 
  11.  11 
  12.  12 /****************************************************************/
  13.  13 /*                      WinMain()                               */
  14.  14 /****************************************************************/
  15.  15 
  16.  16 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  17.  17                    LPSTR lpszCmdLine, int nCmdShow)
  18.  18 {
  19.  19    WNDCLASS wclass;
  20.  20    MSG      msg;
  21.  21    HWND     hWnd;
  22.  22    char     szName[] = "Demo6_1b";
  23.  23 
  24.  24    if (!hPrevInstance)
  25.  25     {
  26.  26         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  27.  27         wclass.lpfnWndProc   = MainWndProc;
  28.  28         wclass.cbClsExtra    = 0;
  29.  29         wclass.cbWndExtra    = 0;
  30.  30         wclass.hInstance     = hInstance;
  31.  31         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  32.  32         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  33.  33         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  34.  34         wclass.lpszMenuName  = szName;
  35.  35         wclass.lpszClassName = szName;
  36.  36 
  37.  37         if (!RegisterClass (&wclass))
  38.  38            return (FALSE);
  39.  39     }
  40.  40 
  41.  41     hWnd = CreateWindow(
  42.  42                 szName,
  43.  43                 "Play MetaFile",
  44.  44                 WS_OVERLAPPEDWINDOW,
  45.  45                 CW_USEDEFAULT,
  46.  46                 CW_USEDEFAULT,
  47.  47                 CW_USEDEFAULT,
  48.  48                 CW_USEDEFAULT,
  49.  49                 NULL,
  50.  50                 NULL,
  51.  51                 hInstance,
  52.  52                 NULL );
  53.  53 
  54.  54     if (!hWnd)
  55.  55         return (FALSE);
  56.  56 
  57.  57     ShowWindow(hWnd, nCmdShow);
  58.  58     UpdateWindow(hWnd);
  59.  59 
  60.  60     while (GetMessage(&msg, NULL, NULL,NULL))
  61.  61        {
  62.  62            TranslateMessage(&msg);
  63.  63            DispatchMessage(&msg);
  64.  64        }
  65.  65     return (msg.wParam);
  66.  66 }
  67.  67 
  68.  68 
  69.  69 
  70.  70 /****************************************************************/
  71.  71 /*                      MainWndProc()                           */
  72.  72 /****************************************************************/
  73.  73 
  74.  74 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  75.  75                             WORD wParam, LONG lParam)
  76.  76 {
  77.  77    PAINTSTRUCT   ps;
  78.  78    HDC           hDC;
  79.  79    static HANDLE hMF;
  80.  80    char          Filename[] = "demo6_1.mf";
  81.  81 
  82.  82    switch (message)
  83.  83     {
  84.  84         case WM_CREATE :
  85.  85                 srand(LOWORD(GetCurrentTime()));
  86.  86                 hMF = GetMetaFile(Filename);
  87.  87                 if (hMF == 0)
  88.  88                   {
  89.  89                      char  Str[100];
  90.  90 
  91.  91                      sprintf(Str, "File %s not found !",
  92.  92                                   Filename);
  93.  93                      MessageBox(hWnd,
  94.  94                                 Str,
  95.  95                                 NULL,
  96.  96                                 MB_OK | MB_ICONHAND);
  97.  97                      DestroyWindow(hWnd);
  98.  98                   }
  99.  99                 return (0);
  100. 100 
  101. 101         case WM_COMMAND :
  102. 102                 switch(wParam)
  103. 103                   {
  104. 104                     case IDM_EXIT :
  105. 105                          DestroyWindow (hWnd);
  106. 106                          break;
  107. 107 
  108. 108                     case IDM_DRAW :
  109. 109                          InvalidateRect(hWnd, NULL, TRUE);
  110. 110                          break;
  111. 111                   }
  112. 112                 return (0);
  113. 113 
  114. 114       case WM_PAINT :
  115. 115             hDC = BeginPaint(hWnd, &ps);
  116. 116 
  117. 117             PlayMetaFile(hDC, hMF);
  118. 118 
  119. 119             EndPaint(hWnd, &ps);
  120. 120             return (0);
  121. 121 
  122. 122       case WM_DESTROY :
  123. 123                 DeleteMetaFile(hMF);
  124. 124                 unlink(Filename);
  125. 125                 PostQuitMessage(0);
  126. 126                 return (0);
  127. 127 
  128. 128       default :
  129. 129          return (DefWindowProc(hWnd, message, wParam, lParam));
  130. 130     }
  131. 131 }