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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo6_1   ---  MetaFile Test                         */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo6_1.h"
  7.   7 
  8.   8 #define MAXPOINTS       30
  9.   9 
  10.  10 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  11.  11 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  12.  12 
  13.  13 
  14.  14 /****************************************************************/
  15.  15 /*                      WinMain()                               */
  16.  16 /****************************************************************/
  17.  17 
  18.  18 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  19.  19                    LPSTR lpszCmdLine, int nCmdShow)
  20.  20 {
  21.  21    WNDCLASS wclass;
  22.  22    MSG      msg;
  23.  23    HWND     hWnd;
  24.  24    char     szName[] = "Demo6_1";
  25.  25 
  26.  26    if (!hPrevInstance)
  27.  27     {
  28.  28         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  29.  29         wclass.lpfnWndProc   = MainWndProc;
  30.  30         wclass.cbClsExtra    = 0;
  31.  31         wclass.cbWndExtra    = 0;
  32.  32         wclass.hInstance     = hInstance;
  33.  33         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  34.  34         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  35.  35         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  36.  36         wclass.lpszMenuName  = szName;
  37.  37         wclass.lpszClassName = szName;
  38.  38 
  39.  39         if (!RegisterClass (&wclass))
  40.  40            return (FALSE);
  41.  41     }
  42.  42 
  43.  43     hWnd = CreateWindow(
  44.  44                 szName,
  45.  45                 "MetaFile Test",
  46.  46                 WS_OVERLAPPEDWINDOW,
  47.  47                 CW_USEDEFAULT,
  48.  48                 CW_USEDEFAULT,
  49.  49                 CW_USEDEFAULT,
  50.  50                 CW_USEDEFAULT,
  51.  51                 NULL,
  52.  52                 NULL,
  53.  53                 hInstance,
  54.  54                 NULL );
  55.  55 
  56.  56     if (!hWnd)
  57.  57         return (FALSE);
  58.  58 
  59.  59     ShowWindow(hWnd, nCmdShow);
  60.  60     UpdateWindow(hWnd);
  61.  61 
  62.  62     while (GetMessage(&msg, NULL, NULL,NULL))
  63.  63        {
  64.  64            TranslateMessage(&msg);
  65.  65            DispatchMessage(&msg);
  66.  66        }
  67.  67     return (msg.wParam);
  68.  68 }
  69.  69 
  70.  70 
  71.  71 
  72.  72 /****************************************************************/
  73.  73 /*                      MainWndProc()                           */
  74.  74 /****************************************************************/
  75.  75 
  76.  76 typedef struct tagSHAPE {
  77.  77            int  x1, y1;
  78.  78            int  x2, y2;
  79.  79            int  ShapeID;
  80.  80 } SHAPE;
  81.  81 
  82.  82 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  83.  83                             WORD wParam, LONG lParam)
  84.  84 {
  85.  85    PAINTSTRUCT   ps;
  86.  86    HDC           hDC;
  87.  87    HMENU         hMenu;
  88.  88    static int    ShapeID = IDM_LINE;
  89.  89    RECT          Rect;
  90.  90    int           x1, x2, y1, y2;
  91.  91    static int    CX, CY;
  92.  92    int           i;
  93.  93    static int    Num = 0;
  94.  94    static SHAPE  Shape[MAXPOINTS];
  95.  95    static HDC    hMetaDC;
  96.  96    char          Filename[] = "demo6_1.mf";
  97.  97 
  98.  98    switch (message)
  99.  99     {
  100. 100         case WM_CREATE :
  101. 101                 srand(LOWORD(GetCurrentTime()));
  102. 102                 hMetaDC = CreateMetaFile(Filename);
  103. 103                 return (0);
  104. 104 
  105. 105         case WM_COMMAND :
  106. 106                 switch(wParam)
  107. 107                   {
  108. 108                     case IDM_EXIT :
  109. 109                          DestroyWindow (hWnd);
  110. 110 
  111. 111                          break;
  112. 112 
  113. 113                     case IDM_LINE :
  114. 114                          if (ShapeID != IDM_LINE)
  115. 115                            {
  116. 116                              hMenu = GetMenu(hWnd);
  117. 117                              CheckMenuItem(hMenu, ShapeID,
  118. 118                                               MF_UNCHECKED);
  119. 119                              ShapeID = IDM_LINE;
  120. 120                              CheckMenuItem(hMenu, ShapeID,
  121. 121                                               MF_CHECKED);
  122. 122                            }
  123. 123                          break;
  124. 124 
  125. 125                     case IDM_RECT :
  126. 126                          if (ShapeID != IDM_RECT)
  127. 127                            {
  128. 128                              hMenu = GetMenu(hWnd);
  129. 129                              CheckMenuItem(hMenu, ShapeID,
  130. 130                                               MF_UNCHECKED);
  131. 131                              ShapeID = IDM_RECT;
  132. 132                              CheckMenuItem(hMenu, ShapeID,
  133. 133                                               MF_CHECKED);
  134. 134                            }
  135. 135                          break;
  136. 136 
  137. 137                     case IDM_ELLIP :
  138. 138                          if (ShapeID != IDM_ELLIP)
  139. 139                            {
  140. 140                              hMenu = GetMenu(hWnd);
  141. 141                              CheckMenuItem(hMenu, ShapeID,
  142. 142                                               MF_UNCHECKED);
  143. 143                              ShapeID = IDM_ELLIP;
  144. 144                              CheckMenuItem(hMenu, ShapeID,
  145. 145                                               MF_CHECKED);
  146. 146                            }
  147. 147                          break;
  148. 148 
  149. 149                     case IDM_DRAW :
  150. 150                          if (Num == MAXPOINTS)
  151. 151                            {
  152. 152                               MessageBox(hWnd,
  153. 153                                 "Memory not enough", "Warning",
  154. 154                                 MB_OK | MB_ICONEXCLAMATION);
  155. 155                               break;
  156. 156                            }
  157. 157 
  158. 158                          hDC = GetDC(hWnd);
  159. 159 
  160. 160                          x1 = rand() % CX;
  161. 161                          y1 = rand() % CY;
  162. 162                          x2 = rand() % CX;
  163. 163                          y2 = rand() % CY;
  164. 164 
  165. 165                          switch (ShapeID)
  166. 166                            {
  167. 167                             case IDM_LINE :
  168. 168                                  MoveTo(hDC, x1, y1);
  169. 169                                  LineTo(hDC, x2, y2);
  170. 170                                  MoveTo(hMetaDC, x1, y1);
  171. 171                                  LineTo(hMetaDC, x2, y2);
  172. 172                                  break;
  173. 173 
  174. 174                             case IDM_RECT :
  175. 175                                  Rectangle(hDC, x1, y1,
  176. 176                                                 x2, y2);
  177. 177                                  Rectangle(hMetaDC, x1, y1,
  178. 178                                                     x2, y2);
  179. 179                                  break;
  180. 180 
  181. 181                             case IDM_ELLIP :
  182. 182                                  Ellipse(hDC, x1, y1,
  183. 183                                               x2, y2);
  184. 184                                  Ellipse(hMetaDC, x1, y1,
  185. 185                                                   x2, y2);
  186. 186                                  break;
  187. 187                            }
  188. 188 
  189. 189                          ReleaseDC(hWnd, hDC);
  190. 190 
  191. 191                          Shape[Num].x1 = x1;
  192. 192                          Shape[Num].y1 = y1;
  193. 193                          Shape[Num].x2 = x2;
  194. 194                          Shape[Num].y2 = y2;
  195. 195                          Shape[Num].ShapeID = ShapeID;
  196. 196                          Num ++;
  197. 197                          break;
  198. 198                   }
  199. 199                 return (0);
  200. 200 
  201. 201       case WM_PAINT :
  202. 202             hDC = BeginPaint(hWnd, &ps);
  203. 203             for (i=0; i<Num; i++)
  204. 204               {
  205. 205                 switch (Shape[i].ShapeID)
  206. 206                  {
  207. 207                    case IDM_LINE :
  208. 208                      MoveTo(hDC, Shape[i].x1, Shape[i].y1);
  209. 209                      LineTo(hDC, Shape[i].x2, Shape[i].y2);
  210. 210                      break;
  211. 211 
  212. 212                    case IDM_RECT :
  213. 213                      Rectangle(hDC, Shape[i].x1, Shape[i].y1,
  214. 214                                     Shape[i].x2, Shape[i].y2);
  215. 215                      break;
  216. 216 
  217. 217                    case IDM_ELLIP :
  218. 218                      Ellipse(hDC, Shape[i].x1, Shape[i].y1,
  219. 219                                   Shape[i].x2, Shape[i].y2);
  220. 220                      break;
  221. 221                  }
  222. 222               }
  223. 223             EndPaint(hWnd, &ps);
  224. 224             return (0);
  225. 225 
  226. 226       case WM_SIZE :
  227. 227                 GetClientRect(hWnd, &Rect);
  228. 228                 CX = Rect.right  - Rect.left;
  229. 229                 CY = Rect.bottom - Rect.top;
  230. 230                 return (0);
  231. 231 
  232. 232       case WM_DESTROY :
  233. 233                 DeleteMetaFile(CloseMetaFile(hMetaDC));
  234. 234                 PostQuitMessage(0);
  235. 235                 return (0) ;
  236. 236 
  237. 237       default :
  238. 238          return (DefWindowProc(hWnd, message, wParam, lParam));
  239. 239     }
  240. 240 }