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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo1_1   ---  The simple example                    */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo1_1.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 /****************************************************************/
  14.  14 /*                      WinMain()                               */
  15.  15 /****************************************************************/
  16.  16 
  17.  17 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  18.  18                    LPSTR lpszCmdLine, int nCmdShow)
  19.  19 {
  20.  20    WNDCLASS wclass;
  21.  21    MSG      msg;
  22.  22    HWND     hWnd;
  23.  23    char     szName[] = "Demo1_1";
  24.  24 
  25.  25    if (!hPrevInstance)
  26.  26     {
  27.  27         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  28.  28         wclass.lpfnWndProc   = MainWndProc;
  29.  29         wclass.cbClsExtra    = 0;
  30.  30         wclass.cbWndExtra    = 0;
  31.  31         wclass.hInstance     = hInstance;
  32.  32         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  33.  33         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  34.  34         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  35.  35         wclass.lpszMenuName  = szName;
  36.  36         wclass.lpszClassName = szName;
  37.  37 
  38.  38         if (!RegisterClass (&wclass))
  39.  39            return (FALSE);
  40.  40     }
  41.  41 
  42.  42     hWnd = CreateWindow(
  43.  43                 szName,
  44.  44                 "The simple example",
  45.  45                 WS_OVERLAPPEDWINDOW,
  46.  46                 CW_USEDEFAULT,
  47.  47                 CW_USEDEFAULT,
  48.  48                 CW_USEDEFAULT,
  49.  49                 CW_USEDEFAULT,
  50.  50                 NULL,
  51.  51                 NULL,
  52.  52                 hInstance,
  53.  53                 NULL );
  54.  54 
  55.  55     if (!hWnd)
  56.  56         return (FALSE);
  57.  57 
  58.  58     ShowWindow(hWnd, nCmdShow);
  59.  59     UpdateWindow(hWnd);
  60.  60 
  61.  61     while (GetMessage(&msg, NULL, NULL,NULL))
  62.  62        {
  63.  63            TranslateMessage(&msg);
  64.  64            DispatchMessage(&msg);
  65.  65        }
  66.  66     return (msg.wParam);
  67.  67 }
  68.  68 
  69.  69 
  70.  70 
  71.  71 /****************************************************************/
  72.  72 /*                      MainWndProc()                           */
  73.  73 /****************************************************************/
  74.  74 
  75.  75 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  76.  76                             WORD wParam, LONG lParam)
  77.  77 {
  78.  78    HDC           hDC;
  79.  79    HMENU         hMenu;
  80.  80    static int    ShapeID = IDM_LINE;
  81.  81    RECT          Rect;
  82.  82    int           x1, x2, y1, y2;
  83.  83    static int    CX, CY;
  84.  84 
  85.  85    switch (message)
  86.  86     {
  87.  87       case WM_CREATE :
  88.  88                 srand(LOWORD(GetCurrentTime()));
  89.  89                 break;
  90.  90 
  91.  91       case WM_COMMAND :
  92.  92                 switch(wParam)
  93.  93                   {
  94.  94                     case IDM_CLEAR :
  95.  95                          InvalidateRect(hWnd, NULL, TRUE);
  96.  96                          UpdateWindow(hWnd);
  97.  97                          break;
  98.  98 
  99.  99                     case IDM_EXIT :
  100. 100                          DestroyWindow (hWnd);
  101. 101                          break;
  102. 102 
  103. 103                     case IDM_LINE :
  104. 104                          if (ShapeID != IDM_LINE)
  105. 105                            {
  106. 106                              hMenu = GetMenu(hWnd);
  107. 107                              CheckMenuItem(hMenu, ShapeID,
  108. 108                                                   MF_UNCHECKED);
  109. 109                              ShapeID = IDM_LINE;
  110. 110                              CheckMenuItem(hMenu, ShapeID,
  111. 111                                                   MF_CHECKED);
  112. 112                            }
  113. 113                          break;
  114. 114 
  115. 115                     case IDM_RECT :
  116. 116                          if (ShapeID != IDM_RECT)
  117. 117                            {
  118. 118                              hMenu = GetMenu(hWnd);
  119. 119                              CheckMenuItem(hMenu, ShapeID,
  120. 120                                                   MF_UNCHECKED);
  121. 121                              ShapeID = IDM_RECT;
  122. 122                              CheckMenuItem(hMenu, ShapeID,
  123. 123                                                   MF_CHECKED);
  124. 124                            }
  125. 125                          break;
  126. 126 
  127. 127                     case IDM_ELLIP :
  128. 128                          if (ShapeID != IDM_ELLIP)
  129. 129                            {
  130. 130                              hMenu = GetMenu(hWnd);
  131. 131                              CheckMenuItem(hMenu, ShapeID,
  132. 132                                                   MF_UNCHECKED);
  133. 133                              ShapeID = IDM_ELLIP;
  134. 134                              CheckMenuItem(hMenu, ShapeID,
  135. 135                                                   MF_CHECKED);
  136. 136                            }
  137. 137                          break;
  138. 138 
  139. 139                     case IDM_DRAW :
  140. 140                          hDC = GetDC(hWnd);
  141. 141 
  142. 142                          x1 = rand() % CX;
  143. 143                          y1 = rand() % CY;
  144. 144                          x2 = rand() % CX;
  145. 145                          y2 = rand() % CY;
  146. 146 
  147. 147                          switch (ShapeID)
  148. 148                            {
  149. 149                             case IDM_LINE :
  150. 150                                  MoveTo(hDC, x1, y1);
  151. 151                                  LineTo(hDC, x2, y2);
  152. 152                                  break;
  153. 153 
  154. 154                             case IDM_RECT :
  155. 155                                  Rectangle(hDC, x1, y1, x2, y2);
  156. 156                                  break;
  157. 157 
  158. 158                             case IDM_ELLIP :
  159. 159                                  Ellipse(hDC, x1, y1, x2, y2);
  160. 160                                  break;
  161. 161                            }
  162. 162 
  163. 163                          ReleaseDC(hWnd, hDC);
  164. 164                          break;
  165. 165                   }
  166. 166                 break;
  167. 167 
  168. 168       case WM_SIZE :
  169. 169                 GetClientRect(hWnd, &Rect);
  170. 170                 CX = Rect.right  - Rect.left;
  171. 171                 CY = Rect.bottom - Rect.top;
  172. 172                 break;
  173. 173 
  174. 174       case WM_DESTROY :
  175. 175                 PostQuitMessage(0);
  176. 176                 break ;
  177. 177 
  178. 178       default :
  179. 179                 return (DefWindowProc(hWnd, message, wParam, lParam));
  180. 180     }
  181. 181    return (NULL);
  182. 182 }