📄 demo4_3.c
字号:
1 /****************************************************************/
2 /* Demo4_3 --- Clock demo */
3 /****************************************************************/
4
5 #include <windows.h>
6 #include <time.h>
7 #include <math.h>
8 #include "demo4_3.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 hour, min, sec;
32 POINT Client;
33
34
35 /****************************************************************/
36 /* WinMain() */
37 /****************************************************************/
38
39 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
40 LPSTR lpszCmdLine, int nCmdShow)
41 {
42 WNDCLASS wclass;
43 MSG msg;
44 HWND hWnd;
45 char szName[] = "Demo4_3";
46 int ReturnID;
47
48 if (!hPrevInstance)
49 {
50 wclass.style = CS_HREDRAW | CS_VREDRAW;
51 wclass.lpfnWndProc = MainWndProc;
52 wclass.cbClsExtra = 0;
53 wclass.cbWndExtra = 0;
54 wclass.hInstance = hInstance;
55 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
56 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
57 wclass.hbrBackground = GetStockObject(LTGRAY_BRUSH);
58 wclass.lpszMenuName = szName;
59 wclass.lpszClassName = szName;
60
61 if (!RegisterClass (&wclass))
62 return (FALSE);
63 }
64
65 hWnd = CreateWindow(
66 szName,
67 "Clock" ,
68 WS_OVERLAPPEDWINDOW,
69 CW_USEDEFAULT,
70 CW_USEDEFAULT,
71 CW_USEDEFAULT,
72 CW_USEDEFAULT,
73 NULL,
74 NULL,
75 hInstance,
76 NULL );
77
78 if (!hWnd)
79 return (FALSE);
80
81 lpTimeProc = MakeProcInstance((FARPROC)TimeProc,
82 hInstance);
83 while (!SetTimer(hWnd, ID_TIMER, 1000, lpTimeProc) )
84 {
85 ReturnID = MessageBox(hWnd,
86 "Too many timers exist",
87 "Warning",
88 MB_RETRYCANCEL |
89 MB_ICONEXCLAMATION);
90
91 if (ReturnID == IDCANCEL)
92 return (FALSE);
93 }
94
95 ShowWindow(hWnd, nCmdShow);
96 UpdateWindow(hWnd);
97
98 while (GetMessage(&msg, NULL, NULL,NULL))
99 {
100 TranslateMessage(&msg);
101 DispatchMessage(&msg);
102 }
103 return (msg.wParam);
104 }
105
106
107
108 /****************************************************************/
109 /* MainWndProc() */
110 /****************************************************************/
111
112 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
113 WORD wParam, LONG lParam)
114 {
115 HDC hDC;
116 HMENU hMenu;
117 PAINTSTRUCT ps;
118 time_t lTime;
119 struct tm *timeofday;
120
121 switch (message)
122 {
123 case WM_CREATE :
124 hMenu = GetMenu(hWnd);
125 CheckMenuItem(hMenu, IDM_CIRCLE, MF_CHECKED);
126 CheckMenuItem(hMenu, IDM_ISO, MF_CHECKED);
127
128 time(&lTime);
129 timeofday = (struct tm *)localtime(&lTime);
130 hour = timeofday->tm_hour;
131 min = timeofday->tm_min;
132 sec = timeofday->tm_sec;
133
134 return (0);
135
136 case WM_COMMAND :
137 hMenu = GetMenu(hWnd);
138 switch (wParam)
139 {
140 case IDM_CIRCLE :
141 case IDM_SQUARE :
142
143 if (TypeID == wParam)
144 return (0);
145
146 CheckMenuItem(hMenu, TypeID,
147 MF_UNCHECKED);
148 TypeID = wParam;
149 CheckMenuItem(hMenu, TypeID,
150 MF_CHECKED);
151
152 InvalidateRect(hWnd, NULL, TRUE);
153 break;
154
155 case IDM_ISO :
156 case IDM_ANISO :
157
158 if (MapModeID == wParam)
159 return (0);
160
161 CheckMenuItem(hMenu, MapModeID,
162 MF_UNCHECKED);
163 MapModeID = wParam;
164 CheckMenuItem(hMenu, MapModeID,
165 MF_CHECKED);
166
167 InvalidateRect(hWnd, NULL, TRUE);
168 break;
169 }
170 return (0);
171
172 case WM_SIZE :
173 Client.x = LOWORD(lParam);
174 Client.y = HIWORD(lParam);
175 return (0);
176
177 case WM_PAINT :
178 hDC = BeginPaint(hWnd, &ps);
179
180 DrawClock(hDC);
181
182 EndPaint(hWnd, &ps);
183 return (0);
184
185 case WM_DESTROY :
186 FreeProcInstance(lpTimeProc);
187 PostQuitMessage(0);
188 return (0);
189
190 default :
191 return(DefWindowProc(hWnd, message, wParam, lParam));
192 }
193 }
194
195
196
197 WORD FAR PASCAL TimeProc(HWND hWnd, unsigned message,
198 WORD wParam, LONG lParam)
199 {
200 time_t lTime;
201 struct tm *timeofday;
202 HDC hDC;
203
204 time(&lTime);
205 timeofday = (struct tm*)localtime(&lTime);
206
207 if (sec != timeofday->tm_sec)
208 {
209
210 hDC = GetDC(hWnd);
211
212 /* Change the time */
213 ChangeTime(hDC, timeofday->tm_hour,
214 timeofday->tm_min, timeofday->tm_sec);
215
216 ReleaseDC(hWnd, hDC);
217 }
218
219 return (0);
220 }
221
222
223
224 void ChangeTime(HDC hDC, int h, int m, int s)
225 {
226 float Rad;
227 POINT Head, Tail, Side1, Side2;
228
229 if (sec == s)
230 return ;
231
232 if (MapModeID == IDM_ISO)
233 SetMapMode(hDC, MM_ISOTROPIC);
234 else
235 SetMapMode(hDC, MM_ANISOTROPIC);
236
237 SetWindowExt(hDC, 200, 200);
238 SetViewportExt(hDC, Client.x, -Client.y);
239
240 SetViewportOrg(hDC, Client.x/2, Client.y/2);
241
242 SetROP2(hDC, R2_NOTXORPEN);
243
244 /* Clear the sec pin */
245 Rad = PI/2 - 2*PI*sec/60;
246 Head.x = secR * cos(Rad);
247 Head.y = secR * sin(Rad);
248 Tail.x = 0;
249 Tail.y = 0;
250 MoveTo(hDC, Head.x, Head.y);
251 LineTo(hDC, Tail.x, Tail.y);
252
253 if (min != m || hour != h)
254 {
255 SetROP2(hDC, R2_NOTCOPYPEN);
256
257 /* Clear the hour pin */
258 Rad = PI/2 - (2*PI*hour/12 + 2*PI*min/60/12);
259 Head.x = hourR * cos(Rad);
260 Head.y = hourR * sin(Rad);
261 Tail.x = TailR * cos(Rad+PI);
262 Tail.y = TailR * sin(Rad+PI);
263 Side1.x = SideR * cos(Rad+PI/2);
264 Side1.y = SideR * sin(Rad+PI/2);
265 Side2.x = SideR * cos(Rad-PI/2);
266 Side2.y = SideR * sin(Rad-PI/2);
267
268 MoveTo(hDC, Head.x, Head.y);
269 LineTo(hDC, Side1.x, Side1.y);
270 LineTo(hDC, Tail.x, Tail.y);
271 LineTo(hDC, Side2.x, Side2.y);
272 LineTo(hDC, Head.x, Head.y);
273
274 /* Clear the min pin */
275 Rad = PI/2 - 2*PI*min/60;
276 Head.x = minR * cos(Rad);
277 Head.y = minR * sin(Rad);
278 Tail.x = TailR * cos(Rad+PI);
279 Tail.y = TailR * sin(Rad+PI);
280 Side1.x = SideR * cos(Rad+PI/2);
281 Side1.y = SideR * sin(Rad+PI/2);
282 Side2.x = SideR * cos(Rad-PI/2);
283 Side2.y = SideR * sin(Rad-PI/2);
284
285 MoveTo(hDC, Head.x, Head.y);
286 LineTo(hDC, Side1.x, Side1.y);
287 LineTo(hDC, Tail.x, Tail.y);
288 LineTo(hDC, Side2.x, Side2.y);
289 LineTo(hDC, Head.x, Head.y);
290
291 /* Update the current hour and min */
292 hour = h;
293 min = m;
294
295 SetROP2(hDC, R2_COPYPEN);
296
297 /* Redraw the hour pin */
298 Rad = PI/2 - (2*PI*hour/12 + 2*PI*min/60/12);
299 Head.x = hourR * cos(Rad);
300 Head.y = hourR * sin(Rad);
301 Tail.x = TailR * cos(Rad+PI);
302 Tail.y = TailR * sin(Rad+PI);
303 Side1.x = SideR * cos(Rad+PI/2);
304 Side1.y = SideR * sin(Rad+PI/2);
305 Side2.x = SideR * cos(Rad-PI/2);
306 Side2.y = SideR * sin(Rad-PI/2);
307
308 MoveTo(hDC, Head.x, Head.y);
309 LineTo(hDC, Side1.x, Side1.y);
310 LineTo(hDC, Tail.x, Tail.y);
311 LineTo(hDC, Side2.x, Side2.y);
312 LineTo(hDC, Head.x, Head.y);
313
314 /* Redraw the min pin */
315 Rad = PI/2 - 2*PI*min/60;
316 Head.x = minR * cos(Rad);
317 Head.y = minR * sin(Rad);
318 Tail.x = TailR * cos(Rad+PI);
319 Tail.y = TailR * sin(Rad+PI);
320 Side1.x = SideR * cos(Rad+PI/2);
321 Side1.y = SideR * sin(Rad+PI/2);
322 Side2.x = SideR * cos(Rad-PI/2);
323 Side2.y = SideR * sin(Rad-PI/2);
324
325 MoveTo(hDC, Head.x, Head.y);
326 LineTo(hDC, Side1.x, Side1.y);
327 LineTo(hDC, Tail.x, Tail.y);
328 LineTo(hDC, Side2.x, Side2.y);
329 LineTo(hDC, Head.x, Head.y);
330 }
331
332 /* Update the current sec */
333 sec = s;
334
335 SetROP2(hDC, R2_NOTXORPEN);
336
337 /* Redraw the sec pin */
338 Rad = PI/2 - 2*PI*sec/60;
339 Head.x = secR * cos(Rad);
340 Head.y = secR * sin(Rad);
341 Tail.x = 0;
342 Tail.y = 0;
343
344 MoveTo(hDC, Head.x, Head.y);
345 LineTo(hDC, Tail.x, Tail.y);
346 }
347
348
349 void DrawTime(HDC hDC)
350 {
351 float Rad;
352 POINT Head, Tail, Side1, Side2;
353
354 /* Draw the hour */
355 Rad = PI/2 - (2*PI*hour/12 + 2*PI*min/60/12);
356 Head.x = hourR * cos(Rad);
357 Head.y = hourR * sin(Rad);
358 Tail.x = TailR * cos(Rad+PI);
359 Tail.y = TailR * sin(Rad+PI);
360 Side1.x = SideR * cos(Rad+PI/2);
361 Side1.y = SideR * sin(Rad+PI/2);
362 Side2.x = SideR * cos(Rad-PI/2);
363 Side2.y = SideR * sin(Rad-PI/2);
364
365 MoveTo(hDC, Head.x, Head.y);
366 LineTo(hDC, Side1.x, Side1.y);
367 LineTo(hDC, Tail.x, Tail.y);
368 LineTo(hDC, Side2.x, Side2.y);
369 LineTo(hDC, Head.x, Head.y);
370
371 /* Draw the min */
372 Rad = PI/2 - 2*PI*min/60;
373 Head.x = minR * cos(Rad);
374 Head.y = minR * sin(Rad);
375 Tail.x = TailR * cos(Rad+PI);
376 Tail.y = TailR * sin(Rad+PI);
377 Side1.x = SideR * cos(Rad+PI/2);
378 Side1.y = SideR * sin(Rad+PI/2);
379 Side2.x = SideR * cos(Rad-PI/2);
380 Side2.y = SideR * sin(Rad-PI/2);
381
382 MoveTo(hDC, Head.x, Head.y);
383 LineTo(hDC, Side1.x, Side1.y);
384 LineTo(hDC, Tail.x, Tail.y);
385 LineTo(hDC, Side2.x, Side2.y);
386 LineTo(hDC, Head.x, Head.y);
387
388 /* Draw the sec */
389 SetROP2(hDC, R2_NOTXORPEN);
390
391 Rad = PI/2 - 2*PI*sec/60;
392 Head.x = secR * cos(Rad);
393 Head.y = secR * sin(Rad);
394 Tail.x = 0;
395 Tail.y = 0;
396
397 MoveTo(hDC, Head.x, Head.y);
398 LineTo(hDC, Tail.x, Tail.y);
399 }
400
401
402
403 void DrawClockFrame(HDC hDC)
404 {
405 POINT Center;
406 int i;
407 float Rad;
408
409 SelectObject(hDC, GetStockObject(WHITE_BRUSH));
410
411 if (TypeID == IDM_CIRCLE)
412 Ellipse(hDC, -97, -97, 97, 97);
413 else
414 Rectangle(hDC, -97, -97, 97, 97);
415
416 SelectObject(hDC, GetStockObject(BLACK_BRUSH));
417
418 for (i=0; i<360; i+=6)
419 {
420 Rad = 2*PI*i/360;
421 Center.x = 90 * cos(Rad);
422 Center.y = 90 * sin(Rad);
423
424 if (i%30 == 0)
425 Rectangle(hDC, Center.x-3, Center.y-3,
426 Center.x+3, Center.y+3);
427 else
428 if (TypeID == IDM_CIRCLE &&
429 Client.x>=100 && Client.y>=100)
430 SetPixel(hDC, Center.x, Center.y,
431 RGB(0, 0, 0));
432 }
433 }
434
435
436
437 void DrawClock(HDC hDC)
438 {
439 if (MapModeID == IDM_ISO)
440 SetMapMode(hDC, MM_ISOTROPIC);
441 else
442 SetMapMode(hDC, MM_ANISOTROPIC);
443
444 SetWindowExt(hDC, 200, 200);
445 SetViewportExt(hDC, Client.x, -Client.y);
446
447 SetViewportOrg(hDC, Client.x/2, Client.y/2);
448
449 DrawClockFrame(hDC);
450 DrawTime(hDC);
451 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -