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