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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo4_6   ---  Clock demo                            */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include <time.h>
  7.   7 #include <math.h>
  8.   8 #include "demo4_6.h"
  9.   9 
  10.  10 #define PI     3.1415926
  11.  11 
  12.  12 #define hourR  50
  13.  13 #define minR   75
  14.  14 #define secR   80
  15.  15 #define TailR  20
  16.  16 #define SideR  5
  17.  17 
  18.  18 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  19.  19 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  20.  20 WORD FAR PASCAL TimeProc(HWND, unsigned, WORD, LONG);
  21.  21 
  22.  22 FARPROC lpTimeProc;
  23.  23 
  24.  24 void ChangeTime(HDC, int, int, int);
  25.  25 void DrawTime(HDC);
  26.  26 void DrawClockFrame(HDC);
  27.  27 void DrawClock(HDC);
  28.  28 
  29.  29 int   TypeID = IDM_CIRCLE;
  30.  30 int   MapModeID = IDM_ISO;
  31.  31 int   BrushID = IDM_GRAY;
  32.  32 int   hour, min, sec;
  33.  33 POINT Client;
  34.  34 
  35.  35 
  36.  36 /****************************************************************/
  37.  37 /*                      WinMain()                               */
  38.  38 /****************************************************************/
  39.  39 
  40.  40 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  41.  41                    LPSTR lpszCmdLine, int nCmdShow)
  42.  42 {
  43.  43    WNDCLASS wclass;
  44.  44    MSG      msg;
  45.  45    HWND     hWnd;
  46.  46    char     szName[] = "Demo4_6";
  47.  47    int      ReturnID;
  48.  48 
  49.  49    if (!hPrevInstance)
  50.  50     {
  51.  51         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  52.  52         wclass.lpfnWndProc   = MainWndProc;
  53.  53         wclass.cbClsExtra    = 0;
  54.  54         wclass.cbWndExtra    = 0;
  55.  55         wclass.hInstance     = hInstance;
  56.  56         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  57.  57         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  58.  58         wclass.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  59.  59         wclass.lpszMenuName  = szName;
  60.  60         wclass.lpszClassName = szName;
  61.  61 
  62.  62         if (!RegisterClass (&wclass))
  63.  63            return (FALSE);
  64.  64     }
  65.  65 
  66.  66     hWnd = CreateWindow(
  67.  67                 szName,
  68.  68                 "Clock" ,
  69.  69                 WS_OVERLAPPEDWINDOW,
  70.  70                 CW_USEDEFAULT,
  71.  71                 CW_USEDEFAULT,
  72.  72                 CW_USEDEFAULT,
  73.  73                 CW_USEDEFAULT,
  74.  74                 NULL,
  75.  75                 NULL,
  76.  76                 hInstance,
  77.  77                 NULL );
  78.  78 
  79.  79     if (!hWnd)
  80.  80         return (FALSE);
  81.  81 
  82.  82     lpTimeProc = MakeProcInstance((FARPROC)TimeProc,
  83.  83                                   hInstance);
  84.  84     while (!SetTimer(hWnd, ID_TIMER, 1000, lpTimeProc) )
  85.  85        {
  86.  86           ReturnID = MessageBox(hWnd,
  87.  87                                 "Too many timers exist",
  88.  88                                 "Warning",
  89.  89                                 MB_RETRYCANCEL |
  90.  90                                 MB_ICONEXCLAMATION);
  91.  91 
  92.  92           if (ReturnID == IDCANCEL)
  93.  93               return (FALSE);
  94.  94        }
  95.  95 
  96.  96     ShowWindow(hWnd, nCmdShow);
  97.  97     UpdateWindow(hWnd);
  98.  98 
  99.  99     while (GetMessage(&msg, NULL, NULL,NULL))
  100. 100        {
  101. 101            TranslateMessage(&msg);
  102. 102            DispatchMessage(&msg);
  103. 103        }
  104. 104     return (msg.wParam);
  105. 105 }
  106. 106 
  107. 107 
  108. 108 
  109. 109 /****************************************************************/
  110. 110 /*                      MainWndProc()                           */
  111. 111 /****************************************************************/
  112. 112 
  113. 113 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  114. 114                             WORD wParam, LONG lParam)
  115. 115 {
  116. 116    HDC           hDC;
  117. 117    HMENU         hMenu;
  118. 118    PAINTSTRUCT   ps;
  119. 119    time_t        lTime;
  120. 120    struct tm     *timeofday;
  121. 121 
  122. 122    switch (message)
  123. 123     {
  124. 124       case WM_CREATE :
  125. 125                 hMenu = GetMenu(hWnd);
  126. 126                 CheckMenuItem(hMenu, IDM_CIRCLE, MF_CHECKED);
  127. 127                 CheckMenuItem(hMenu, IDM_ISO, MF_CHECKED);
  128. 128                 CheckMenuItem(hMenu, IDM_GRAY, MF_CHECKED);
  129. 129 
  130. 130                 time(&lTime);
  131. 131                 timeofday = (struct tm *)localtime(&lTime);
  132. 132                 hour = timeofday->tm_hour;
  133. 133                 min  = timeofday->tm_min;
  134. 134                 sec  = timeofday->tm_sec;
  135. 135 
  136. 136                 return (0);
  137. 137 
  138. 138       case WM_COMMAND :
  139. 139                 hMenu = GetMenu(hWnd);
  140. 140                 switch (wParam)
  141. 141                   {
  142. 142                     case IDM_CIRCLE :
  143. 143                     case IDM_SQUARE :
  144. 144 
  145. 145                          if (TypeID == wParam)
  146. 146                             return (0);
  147. 147 
  148. 148                          CheckMenuItem(hMenu, TypeID,
  149. 149                                         MF_UNCHECKED);
  150. 150                          TypeID = wParam;
  151. 151                          CheckMenuItem(hMenu, TypeID,
  152. 152                                         MF_CHECKED);
  153. 153 
  154. 154                          InvalidateRect(hWnd, NULL, TRUE);
  155. 155                          break;
  156. 156 
  157. 157                     case IDM_ISO :
  158. 158                     case IDM_ANISO :
  159. 159 
  160. 160                          if (MapModeID == wParam)
  161. 161                             return (0);
  162. 162 
  163. 163                          CheckMenuItem(hMenu, MapModeID,
  164. 164                                         MF_UNCHECKED);
  165. 165                          MapModeID = wParam;
  166. 166                          CheckMenuItem(hMenu, MapModeID,
  167. 167                                         MF_CHECKED);
  168. 168 
  169. 169                          InvalidateRect(hWnd, NULL, TRUE);
  170. 170                          break;
  171. 171 
  172. 172                     case IDM_RED :
  173. 173                     case IDM_GREEN :
  174. 174                     case IDM_BLUE :
  175. 175                     case IDM_YELLOW :
  176. 176                     case IDM_CYAN :
  177. 177                     case IDM_MAGENTA :
  178. 178                     case IDM_WHITE :
  179. 179                     case IDM_GRAY :
  180. 180                     case IDM_BLACK :
  181. 181 
  182. 182                          if (BrushID == wParam)
  183. 183                             return (0);
  184. 184 
  185. 185                          CheckMenuItem(hMenu, BrushID,
  186. 186                                         MF_UNCHECKED);
  187. 187                          BrushID = wParam;
  188. 188                          CheckMenuItem(hMenu, BrushID,
  189. 189                                         MF_CHECKED);
  190. 190 
  191. 191                          InvalidateRect(hWnd, NULL, TRUE);
  192. 192                          break;
  193. 193 
  194. 194                   }
  195. 195                 return (0);
  196. 196 
  197. 197       case WM_SIZE :
  198. 198                 Client.x = LOWORD(lParam);
  199. 199                 Client.y = HIWORD(lParam);
  200. 200                 return (0);
  201. 201 
  202. 202       case WM_PAINT :
  203. 203                 hDC = BeginPaint(hWnd, &ps);
  204. 204 
  205. 205                 DrawClock(hDC);
  206. 206 
  207. 207                 EndPaint(hWnd, &ps);
  208. 208                 return (0);
  209. 209 
  210. 210       case WM_DESTROY :
  211. 211                 FreeProcInstance(lpTimeProc);
  212. 212                 PostQuitMessage(0);
  213. 213                 return (0);
  214. 214 
  215. 215       default :
  216. 216          return(DefWindowProc(hWnd, message, wParam, lParam));
  217. 217     }
  218. 218 }
  219. 219 
  220. 220 
  221. 221 
  222. 222 WORD FAR PASCAL TimeProc(HWND hWnd, unsigned message,
  223. 223                          WORD wParam, LONG lParam)
  224. 224 {
  225. 225    time_t      lTime;
  226. 226    struct tm   *timeofday;
  227. 227    HDC         hDC;
  228. 228 
  229. 229    time(&lTime);
  230. 230    timeofday = (struct tm*)localtime(&lTime);
  231. 231 
  232. 232    if (sec != timeofday->tm_sec)
  233. 233      {
  234. 234 
  235. 235         hDC = GetDC(hWnd);
  236. 236 
  237. 237         /*  Change the time  */
  238. 238         ChangeTime(hDC, timeofday->tm_hour,
  239. 239                    timeofday->tm_min, timeofday->tm_sec);
  240. 240 
  241. 241         ReleaseDC(hWnd, hDC);
  242. 242      }
  243. 243 
  244. 244    return (0);
  245. 245 }
  246. 246 
  247. 247 
  248. 248 
  249. 249 HANDLE SelectBrush(hDC)
  250. 250 {
  251. 251    COLORREF  rgbColor;
  252. 252    HANDLE    hBrush;
  253. 253 
  254. 254    switch (BrushID)
  255. 255     {
  256. 256       case IDM_RED :
  257. 257                 rgbColor = RGB(255, 0, 0);
  258. 258                 break;
  259. 259 
  260. 260       case IDM_GREEN :
  261. 261                 rgbColor = RGB(0, 255, 0);
  262. 262                 break;
  263. 263 
  264. 264       case IDM_BLUE :
  265. 265                 rgbColor = RGB(0, 0, 255);
  266. 266                 break;
  267. 267 
  268. 268       case IDM_YELLOW :
  269. 269                 rgbColor = RGB(255, 255, 0);
  270. 270                 break;
  271. 271 
  272. 272       case IDM_CYAN :
  273. 273                 rgbColor = RGB(0, 255, 255);
  274. 274                 break;
  275. 275 
  276. 276       case IDM_MAGENTA :
  277. 277                 rgbColor = RGB(255, 0, 255);
  278. 278                 break;
  279. 279     }
  280. 280 
  281. 281    if (BrushID < IDM_WHITE)
  282. 282       hBrush = CreateSolidBrush(rgbColor);
  283. 283    else
  284. 284      {
  285. 285         if (BrushID == IDM_WHITE)
  286. 286           hBrush = GetStockObject(WHITE_BRUSH);
  287. 287         else
  288. 288         if (BrushID == IDM_GRAY)
  289. 289           hBrush = GetStockObject(LTGRAY_BRUSH);
  290. 290         else
  291. 291           hBrush = GetStockObject(BLACK_BRUSH);
  292. 292      }
  293. 293 
  294. 294    return(SelectObject(hDC, hBrush));
  295. 295 }
  296. 296 
  297. 297 
  298. 298 
  299. 299 void ChangeTime(HDC hDC, int h, int m, int s)
  300. 300 {
  301. 301    float  Rad;
  302. 302    POINT  Head, Tail, Side1, Side2;
  303. 303    POINT  Poly[4];
  304. 304    HANDLE hBrush, hPreBrush;
  305. 305    HANDLE hPen;
  306. 306 
  307. 307    if (sec == s)
  308. 308       return ;
  309. 309 
  310. 310    if (MapModeID == IDM_ISO)
  311. 311        SetMapMode(hDC, MM_ISOTROPIC);
  312. 312    else
  313. 313        SetMapMode(hDC, MM_ANISOTROPIC);
  314. 314 
  315. 315    SetWindowExt(hDC, 200, 200);
  316. 316    SetViewportExt(hDC, Client.x, -Client.y);
  317. 317 
  318. 318    SetViewportOrg(hDC, Client.x/2, Client.y/2);
  319. 319 
  320. 320    SetROP2(hDC, R2_NOTXORPEN);
  321. 321 
  322. 322    /*   Clear the sec pin   */
  323. 323    Rad = PI/2 - 2*PI*sec/60;
  324. 324    Head.x  = secR  * cos(Rad);
  325. 325    Head.y  = secR  * sin(Rad);
  326. 326    Tail.x  = 0;
  327. 327    Tail.y  = 0;
  328. 328    MoveTo(hDC, Head.x, Head.y);
  329. 329    LineTo(hDC, Tail.x, Tail.y);
  330. 330 
  331. 331    if (min != m || hour != h)
  332. 332      {
  333. 333         SetROP2(hDC, R2_COPYPEN);
  334. 334 
  335. 335         hBrush = GetStockObject(WHITE_BRUSH);
  336. 336         SelectObject(hDC, hBrush);
  337. 337         hPen = GetStockObject(WHITE_PEN);
  338. 338         SelectObject(hDC, hPen);
  339. 339 
  340. 340         /*   Clear the hour pin   */
  341. 341         Rad = PI/2 - (2*PI*hour/12 + 2*PI*min/60/12);
  342. 342         Head.x  = hourR * cos(Rad);
  343. 343         Head.y  = hourR * sin(Rad);
  344. 344         Tail.x  = TailR * cos(Rad+PI);
  345. 345         Tail.y  = TailR * sin(Rad+PI);
  346. 346         Side1.x = SideR * cos(Rad+PI/2);
  347. 347         Side1.y = SideR * sin(Rad+PI/2);
  348. 348         Side2.x = SideR * cos(Rad-PI/2);
  349. 349         Side2.y = SideR * sin(Rad-PI/2);
  350. 350 
  351. 351         Poly[0] = Head;
  352. 352         Poly[1] = Side1;
  353. 353         Poly[2] = Tail;
  354. 354         Poly[3] = Side2;
  355. 355         Polygon(hDC, Poly, 4);
  356. 356 
  357. 357         /*   Clear the min pin   */
  358. 358         Rad = PI/2 - 2*PI*min/60;
  359. 359         Head.x  = minR  * cos(Rad);
  360. 360         Head.y  = minR  * sin(Rad);
  361. 361         Tail.x  = TailR * cos(Rad+PI);
  362. 362         Tail.y  = TailR * sin(Rad+PI);
  363. 363         Side1.x = SideR * cos(Rad+PI/2);
  364. 364         Side1.y = SideR * sin(Rad+PI/2);
  365. 365         Side2.x = SideR * cos(Rad-PI/2);
  366. 366         Side2.y = SideR * sin(Rad-PI/2);
  367. 367 
  368. 368         Poly[0] = Head;
  369. 369         Poly[1] = Side1;
  370. 370         Poly[2] = Tail;
  371. 371         Poly[3] = Side2;
  372. 372         Polygon(hDC, Poly, 4);
  373. 373 
  374. 374         /*   Update the current hour and min   */
  375. 375         hour = h;
  376. 376         min  = m;
  377. 377 
  378. 378         /*   Create the brush that matches   */
  379. 379         /*      the Brush color menu         */
  380. 380         hPreBrush = SelectBrush(hDC);
  381. 381 
  382. 382         hPen = GetStockObject(BLACK_PEN);
  383. 383         SelectObject(hDC, hPen);
  384. 384 
  385. 385         /*   Redraw the hour pin   */
  386. 386         Rad = PI/2 - (2*PI*hour/12 + 2*PI*min/60/12);
  387. 387         Head.x  = hourR * cos(Rad);
  388. 388         Head.y  = hourR * sin(Rad);
  389. 389         Tail.x  = TailR * cos(Rad+PI);
  390. 390         Tail.y  = TailR * sin(Rad+PI);
  391. 391         Side1.x = SideR * cos(Rad+PI/2);
  392. 392         Side1.y = SideR * sin(Rad+PI/2);
  393. 393         Side2.x = SideR * cos(Rad-PI/2);
  394. 394         Side2.y = SideR * sin(Rad-PI/2);
  395. 395 
  396. 396         Poly[0] = Head;
  397. 397         Poly[1] = Side1;
  398. 398         Poly[2] = Tail;
  399. 399         Poly[3] = Side2;
  400. 400         Polygon(hDC, Poly, 4);
  401. 401 
  402. 402         /*   Redraw the min pin   */
  403. 403         Rad = PI/2 - 2*PI*min/60;
  404. 404         Head.x  = minR  * cos(Rad);
  405. 405         Head.y  = minR  * sin(Rad);
  406. 406         Tail.x  = TailR * cos(Rad+PI);
  407. 407         Tail.y  = TailR * sin(Rad+PI);
  408. 408         Side1.x = SideR * cos(Rad+PI/2);
  409. 409         Side1.y = SideR * sin(Rad+PI/2);
  410. 410         Side2.x = SideR * cos(Rad-PI/2);
  411. 411         Side2.y = SideR * sin(Rad-PI/2);
  412. 412 
  413. 413         Poly[0] = Head;
  414. 414         Poly[1] = Side1;
  415. 415         Poly[2] = Tail;
  416. 416         Poly[3] = Side2;
  417. 417         Polygon(hDC, Poly, 4);
  418. 418 
  419. 419         hBrush = SelectObject(hDC, hPreBrush);
  420. 420         if (BrushID < IDM_WHITE)
  421. 421              DeleteObject(hBrush);
  422. 422      }
  423. 423 
  424. 424    /*   Update the current sec   */
  425. 425    sec = s;
  426. 426 
  427. 427    SetROP2(hDC, R2_NOTXORPEN);
  428. 428 
  429. 429    /*   Redraw the sec pin   */
  430. 430    Rad = PI/2 - 2*PI*sec/60;
  431. 431    Head.x  = secR  * cos(Rad);
  432. 432    Head.y  = secR  * sin(Rad);
  433. 433    Tail.x  = 0;
  434. 434    Tail.y  = 0;
  435. 435 
  436. 436    MoveTo(hDC, Head.x, Head.y);
  437. 437    LineTo(hDC, Tail.x, Tail.y);
  438. 438 }
  439. 439 
  440. 440 
  441. 441 void DrawTime(HDC hDC)
  442. 442 {
  443. 443    float  Rad;
  444. 444    POINT  Head, Tail, Side1, Side2;
  445. 445    POINT  Poly[4];
  446. 446    HANDLE hBrush, hPreBrush;
  447. 447 
  448. 448    /*   Create the brush that matches   */
  449. 449    /*      the Brush color menu      */
  450. 450    hPreBrush = SelectBrush(hDC);
  451. 451 
  452. 452    /*   Draw the hour   */
  453. 453    Rad = PI/2 - (2*PI*hour/12 + 2*PI*min/60/12);
  454. 454    Head.x  = hourR * cos(Rad);
  455. 455    Head.y  = hourR * sin(Rad);
  456. 456    Tail.x  = TailR * cos(Rad+PI);
  457. 457    Tail.y  = TailR * sin(Rad+PI);
  458. 458    Side1.x = SideR * cos(Rad+PI/2);
  459. 459    Side1.y = SideR * sin(Rad+PI/2);
  460. 460    Side2.x = SideR * cos(Rad-PI/2);
  461. 461    Side2.y = SideR * sin(Rad-PI/2);
  462. 462 
  463. 463    Poly[0] = Head;
  464. 464    Poly[1] = Side1;
  465. 465    Poly[2] = Tail;
  466. 466    Poly[3] = Side2;
  467. 467    Polygon(hDC, Poly, 4);
  468. 468 
  469. 469    /*   Draw the min   */
  470. 470    Rad = PI/2 - 2*PI*min/60;
  471. 471    Head.x  = minR  * cos(Rad);
  472. 472    Head.y  = minR  * sin(Rad);
  473. 473    Tail.x  = TailR * cos(Rad+PI);
  474. 474    Tail.y  = TailR * sin(Rad+PI);
  475. 475    Side1.x = SideR * cos(Rad+PI/2);
  476. 476    Side1.y = SideR * sin(Rad+PI/2);
  477. 477    Side2.x = SideR * cos(Rad-PI/2);
  478. 478    Side2.y = SideR * sin(Rad-PI/2);
  479. 479 
  480. 480    Poly[0] = Head;
  481. 481    Poly[1] = Side1;
  482. 482    Poly[2] = Tail;
  483. 483    Poly[3] = Side2;
  484. 484    Polygon(hDC, Poly, 4);
  485. 485 
  486. 486    hBrush = SelectObject(hDC, hPreBrush);
  487. 487    if (BrushID < IDM_WHITE)
  488. 488         DeleteObject(hBrush);
  489. 489 
  490. 490    /*   Draw the sec   */
  491. 491    SetROP2(hDC, R2_NOTXORPEN);
  492. 492 
  493. 493    Rad     = PI/2 - 2*PI*sec/60;
  494. 494    Head.x  = secR  * cos(Rad);
  495. 495    Head.y  = secR  * sin(Rad);
  496. 496    Tail.x  = 0;
  497. 497    Tail.y  = 0;
  498. 498 
  499. 499    MoveTo(hDC, Head.x, Head.y);
  500. 500    LineTo(hDC, Tail.x, Tail.y);
  501. 501 }
  502. 502 
  503. 503 
  504. 504 
  505. 505 void DrawClockFrame(HDC hDC)
  506. 506 {
  507. 507    POINT  Center;
  508. 508    int    i;
  509. 509    float  Rad;
  510. 510 
  511. 511    SelectObject(hDC, GetStockObject(WHITE_BRUSH));
  512. 512 
  513. 513    if (TypeID == IDM_CIRCLE)
  514. 514       Ellipse(hDC, -97, -97, 97, 97);
  515. 515    else
  516. 516       Rectangle(hDC, -97, -97, 97, 97);
  517. 517 
  518. 518    SelectObject(hDC, GetStockObject(BLACK_BRUSH));
  519. 519 
  520. 520    for (i=0; i<360; i+=6)
  521. 521      {
  522. 522         Rad = 2*PI*i/360;
  523. 523         Center.x = 90 * cos(Rad);
  524. 524         Center.y = 90 * sin(Rad);
  525. 525 
  526. 526         if (i%30 == 0)
  527. 527             Rectangle(hDC, Center.x-3, Center.y-3,
  528. 528                            Center.x+3, Center.y+3);
  529. 529         else
  530. 530         if (TypeID == IDM_CIRCLE &&
  531. 531                Client.x>=100 && Client.y>=100)
  532. 532             SetPixel(hDC, Center.x, Center.y,
  533. 533                      RGB(0, 0, 0));
  534. 534      }
  535. 535 }
  536. 536 
  537. 537 
  538. 538 
  539. 539 void DrawClock(HDC hDC)
  540. 540 {
  541. 541    if (MapModeID == IDM_ISO)
  542. 542        SetMapMode(hDC, MM_ISOTROPIC);
  543. 543    else
  544. 544        SetMapMode(hDC, MM_ANISOTROPIC);
  545. 545 
  546. 546    SetWindowExt(hDC, 200, 200);
  547. 547    SetViewportExt(hDC, Client.x, -Client.y);
  548. 548 
  549. 549    SetViewportOrg(hDC, Client.x/2, Client.y/2);
  550. 550 
  551. 551    DrawClockFrame(hDC);
  552. 552    DrawTime(hDC);
  553. 553 }