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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo1_4   ---  The simple example                    */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo1_4.h"
  7.   7 
  8.   8 #define MAXPOINTS       30
  9.   9 #define MaxX          1000
  10.  10 #define MaxY          1000
  11.  11 
  12.  12 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  13.  13 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  14.  14 
  15.  15 
  16.  16 
  17.  17 /****************************************************************/
  18.  18 /*                      WinMain()                               */
  19.  19 /****************************************************************/
  20.  20 
  21.  21 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  22.  22                    LPSTR lpszCmdLine, int nCmdShow)
  23.  23 {
  24.  24    WNDCLASS wclass;
  25.  25    MSG      msg;
  26.  26    HWND     hWnd;
  27.  27    char     szName[] = "Demo1_4";
  28.  28 
  29.  29    if (!hPrevInstance)
  30.  30     {
  31.  31         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  32.  32         wclass.lpfnWndProc   = MainWndProc;
  33.  33         wclass.cbClsExtra    = 0;
  34.  34         wclass.cbWndExtra    = 0;
  35.  35         wclass.hInstance     = hInstance;
  36.  36         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  37.  37         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  38.  38         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  39.  39         wclass.lpszMenuName  = szName;
  40.  40         wclass.lpszClassName = szName;
  41.  41 
  42.  42         if (!RegisterClass (&wclass))
  43.  43            return (FALSE);
  44.  44     }
  45.  45 
  46.  46     hWnd = CreateWindow(
  47.  47                 szName,
  48.  48                 "The simple example - V.3",
  49.  49                 WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  50.  50                 CW_USEDEFAULT,
  51.  51                 CW_USEDEFAULT,
  52.  52                 CW_USEDEFAULT,
  53.  53                 CW_USEDEFAULT,
  54.  54                 NULL,
  55.  55                 NULL,
  56.  56                 hInstance,
  57.  57                 NULL );
  58.  58 
  59.  59     if (!hWnd)
  60.  60         return (FALSE);
  61.  61 
  62.  62     ShowWindow(hWnd, nCmdShow);
  63.  63     UpdateWindow(hWnd);
  64.  64 
  65.  65     while (GetMessage(&msg, NULL, NULL,NULL))
  66.  66        {
  67.  67            TranslateMessage(&msg);
  68.  68            DispatchMessage(&msg);
  69.  69        }
  70.  70     return (msg.wParam);
  71.  71 }
  72.  72 
  73.  73 
  74.  74 
  75.  75 /****************************************************************/
  76.  76 /*                      MainWndProc()                           */
  77.  77 /****************************************************************/
  78.  78 
  79.  79 typedef struct tagSHAPE {
  80.  80            int  x1, y1;
  81.  81            int  x2, y2;
  82.  82            int  ShapeID;
  83.  83 } SHAPE;
  84.  84 
  85.  85 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  86.  86                             WORD wParam, LONG lParam)
  87.  87 {
  88.  88    PAINTSTRUCT   ps;
  89.  89    HDC           hDC;
  90.  90    HMENU         hMenu;
  91.  91    static int    ShapeID = IDM_LINE;
  92.  92    RECT          Rect, dstRect;
  93.  93    int           x1, x2, y1, y2;
  94.  94    static int    CX, CY;
  95.  95    int           i, Temp;
  96.  96    static int    Num = 0;
  97.  97    static SHAPE  Shape[MAXPOINTS];
  98.  98    static int    OX, OY;
  99.  99 
  100. 100    switch (message)
  101. 101     {
  102. 102         case WM_CREATE :
  103. 103                 srand(LOWORD(GetCurrentTime()));
  104. 104                 break;
  105. 105 
  106. 106         case WM_COMMAND :
  107. 107                 switch(wParam)
  108. 108                   {
  109. 109                     case IDM_CLEAR :
  110. 110                          Num = 0;
  111. 111                          InvalidateRect(hWnd, NULL, TRUE);
  112. 112                          UpdateWindow(hWnd);
  113. 113                          break;
  114. 114 
  115. 115                     case IDM_EXIT :
  116. 116                          DestroyWindow (hWnd);
  117. 117                          break;
  118. 118 
  119. 119                     case IDM_LINE :
  120. 120                          if (ShapeID != IDM_LINE)
  121. 121                            {
  122. 122                              hMenu = GetMenu(hWnd);
  123. 123                              CheckMenuItem(hMenu, ShapeID,
  124. 124                                                   MF_UNCHECKED);
  125. 125                              ShapeID = IDM_LINE;
  126. 126                              CheckMenuItem(hMenu, ShapeID,
  127. 127                                                   MF_CHECKED);
  128. 128                            }
  129. 129                          break;
  130. 130 
  131. 131                     case IDM_RECT :
  132. 132                          if (ShapeID != IDM_RECT)
  133. 133                            {
  134. 134                              hMenu = GetMenu(hWnd);
  135. 135                              CheckMenuItem(hMenu, ShapeID,
  136. 136                                                   MF_UNCHECKED);
  137. 137                              ShapeID = IDM_RECT;
  138. 138                              CheckMenuItem(hMenu, ShapeID,
  139. 139                                                   MF_CHECKED);
  140. 140                            }
  141. 141                          break;
  142. 142 
  143. 143                     case IDM_ELLIP :
  144. 144                          if (ShapeID != IDM_ELLIP)
  145. 145                            {
  146. 146                              hMenu = GetMenu(hWnd);
  147. 147                              CheckMenuItem(hMenu, ShapeID,
  148. 148                                                   MF_UNCHECKED);
  149. 149                              ShapeID = IDM_ELLIP;
  150. 150                              CheckMenuItem(hMenu, ShapeID,
  151. 151                                                   MF_CHECKED);
  152. 152                            }
  153. 153                          break;
  154. 154 
  155. 155                     case IDM_DRAW :
  156. 156                          if (Num == MAXPOINTS)
  157. 157                            {
  158. 158                               MessageBox(hWnd,
  159. 159                                 "Memory not enough", "Warning",
  160. 160                                 MB_OK | MB_ICONEXCLAMATION);
  161. 161                               break;
  162. 162                            }
  163. 163 
  164. 164                          hDC = GetDC(hWnd);
  165. 165 
  166. 166                          x1 = rand() % CX;
  167. 167                          y1 = rand() % CY;
  168. 168                          x2 = rand() % CX;
  169. 169                          y2 = rand() % CY;
  170. 170 
  171. 171                          switch (ShapeID)
  172. 172                            {
  173. 173                             case IDM_LINE :
  174. 174                                  MoveTo(hDC, x1, y1);
  175. 175                                  LineTo(hDC, x2, y2);
  176. 176                                  break;
  177. 177 
  178. 178                             case IDM_RECT :
  179. 179                                  Rectangle(hDC, x1, y1, x2, y2);
  180. 180                                  break;
  181. 181 
  182. 182                             case IDM_ELLIP :
  183. 183                                  Ellipse(hDC, x1, y1, x2, y2);
  184. 184                                  break;
  185. 185                            }
  186. 186 
  187. 187                          ReleaseDC(hWnd, hDC);
  188. 188 
  189. 189                          Shape[Num].x1 = OX + x1;
  190. 190                          Shape[Num].y1 = OY + y1;
  191. 191                          Shape[Num].x2 = OX + x2;
  192. 192                          Shape[Num].y2 = OY + y2;
  193. 193                          Shape[Num].ShapeID = ShapeID;
  194. 194                          Num ++;
  195. 195                          break;
  196. 196                   }
  197. 197                 break;
  198. 198 
  199. 199       case WM_PAINT :
  200. 200             hDC = BeginPaint(hWnd, &ps);
  201. 201             for (i=0; i<Num; i++)
  202. 202               {
  203. 203                 Rect.left  = min(Shape[i].x1-OX, Shape[i].x2-OX);
  204. 204                 Rect.right = max(Shape[i].x1-OX, Shape[i].x2-OX);
  205. 205                 Rect.top   = min(Shape[i].y1-OY, Shape[i].y2-OY);
  206. 206                 Rect.bottom= max(Shape[i].y1-OY, Shape[i].y2-OY);
  207. 207                 IntersectRect(&dstRect, &Rect, &(ps.rcPaint));
  208. 208                 if (IsRectEmpty(&dstRect))
  209. 209                     continue;
  210. 210 
  211. 211                 switch (Shape[i].ShapeID)
  212. 212                  {
  213. 213                    case IDM_LINE :
  214. 214                      MoveTo(hDC, Shape[i].x1-OX, Shape[i].y1-OY);
  215. 215                      LineTo(hDC, Shape[i].x2-OX, Shape[i].y2-OY);
  216. 216                      break;
  217. 217 
  218. 218                    case IDM_RECT :
  219. 219                      Rectangle(hDC, Shape[i].x1-OX, Shape[i].y1-OY,
  220. 220                                     Shape[i].x2-OX, Shape[i].y2-OY);
  221. 221                      break;
  222. 222 
  223. 223                    case IDM_ELLIP :
  224. 224                      Ellipse(hDC, Shape[i].x1-OX, Shape[i].y1-OY,
  225. 225                                   Shape[i].x2-OX, Shape[i].y2-OY);
  226. 226                      break;
  227. 227                  }
  228. 228               }
  229. 229             EndPaint(hWnd, &ps);
  230. 230             break;
  231. 231 
  232. 232       case WM_SIZE :
  233. 233                 GetClientRect(hWnd, &Rect);
  234. 234                 CX = Rect.right  - Rect.left;
  235. 235                 CY = Rect.bottom - Rect.top;
  236. 236                 SetScrollRange(hWnd, SB_HORZ, 0, MaxX-CX, TRUE);
  237. 237                 SetScrollRange(hWnd, SB_VERT, 0, MaxY-CY, TRUE);
  238. 238                 OX = min(OX, MaxX-CX);
  239. 239                 OY = min(OY, MaxY-CY);
  240. 240                 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
  241. 241                 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
  242. 242                 break;
  243. 243 
  244. 244       case WM_KEYDOWN :
  245. 245                 switch (wParam)
  246. 246                   {
  247. 247                     case VK_HOME :
  248. 248                         SendMessage(hWnd, WM_HSCROLL,
  249. 249                                     SB_TOP, 0L);
  250. 250                         break;
  251. 251 
  252. 252                     case VK_END :
  253. 253                         SendMessage(hWnd, WM_HSCROLL,
  254. 254                                     SB_BOTTOM, 0L);
  255. 255                         break;
  256. 256 
  257. 257                     case VK_PRIOR :
  258. 258                         SendMessage(hWnd, WM_VSCROLL,
  259. 259                                     SB_TOP, 0L);
  260. 260                         break;
  261. 261 
  262. 262                     case VK_NEXT :
  263. 263                         SendMessage(hWnd, WM_VSCROLL,
  264. 264                                     SB_BOTTOM, 0L);
  265. 265                         break;
  266. 266 
  267. 267                     case VK_LEFT :
  268. 268                         SendMessage(hWnd, WM_HSCROLL,
  269. 269                                     SB_LINEUP, 0L);
  270. 270                         break;
  271. 271 
  272. 272                     case VK_RIGHT :
  273. 273                         SendMessage(hWnd, WM_HSCROLL,
  274. 274                                     SB_LINEDOWN, 0L);
  275. 275                         break;
  276. 276 
  277. 277                     case VK_UP :
  278. 278                         SendMessage(hWnd, WM_VSCROLL,
  279. 279                                     SB_LINEUP, 0L);
  280. 280                         break;
  281. 281 
  282. 282                     case VK_DOWN :
  283. 283                         SendMessage(hWnd, WM_VSCROLL,
  284. 284                                     SB_LINEDOWN, 0L);
  285. 285                         break;
  286. 286                   }
  287. 287                 break;
  288. 288 
  289. 289       case WM_HSCROLL :
  290. 290                 switch (wParam)
  291. 291                   {
  292. 292                     case SB_TOP :
  293. 293                         Temp = 0;
  294. 294                         break;
  295. 295 
  296. 296                     case SB_BOTTOM :
  297. 297                         Temp = MaxX - CX;
  298. 298                         break;
  299. 299 
  300. 300                     case SB_LINEUP :
  301. 301                     case SB_PAGEUP :
  302. 302                         Temp = OX - CX/4;
  303. 303                         break;
  304. 304 
  305. 305                     case SB_LINEDOWN :
  306. 306                     case SB_PAGEDOWN :
  307. 307                         Temp = OX + CX/4;
  308. 308                         break;
  309. 309 
  310. 310                     case SB_THUMBPOSITION :
  311. 311                         Temp = LOWORD(lParam);
  312. 312                         break;
  313. 313 
  314. 314                     default :
  315. 315                         return (NULL);
  316. 316                   }
  317. 317                 Temp = min(max(0, Temp), MaxX-CX);
  318. 318                 if (Temp == OX) break;
  319. 319 
  320. 320                 OX = Temp;
  321. 321                 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
  322. 322                 InvalidateRect(hWnd, NULL, TRUE);
  323. 323                 break;
  324. 324 
  325. 325       case WM_VSCROLL :
  326. 326                 switch (wParam)
  327. 327                   {
  328. 328                     case SB_TOP :
  329. 329                         Temp = 0;
  330. 330                         break;
  331. 331 
  332. 332                     case SB_BOTTOM :
  333. 333                         Temp = MaxY - CY;
  334. 334                         break;
  335. 335 
  336. 336                     case SB_LINEUP :
  337. 337                     case SB_PAGEUP :
  338. 338                         Temp = OY - CY/4;
  339. 339                         break;
  340. 340 
  341. 341                     case SB_LINEDOWN :
  342. 342                     case SB_PAGEDOWN :
  343. 343                         Temp = OY + CY/4;
  344. 344                         break;
  345. 345 
  346. 346                     case SB_THUMBPOSITION :
  347. 347                         Temp = LOWORD(lParam);
  348. 348                         break;
  349. 349 
  350. 350                     default :
  351. 351                         return (NULL);
  352. 352                   }
  353. 353                 Temp = min(max(0, Temp), MaxY-CY);
  354. 354                 if (Temp == OY) break;
  355. 355 
  356. 356                 OY = Temp;
  357. 357                 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
  358. 358                 InvalidateRect(hWnd, NULL, TRUE);
  359. 359                 break;
  360. 360 
  361. 361       case WM_DESTROY :
  362. 362                 PostQuitMessage(0);
  363. 363                 break ;
  364. 364 
  365. 365       default :
  366. 366                 return (DefWindowProc(hWnd, message, wParam, lParam));
  367. 367     }
  368. 368    return (NULL);
  369. 369 }