模擬時鐘,時鐘
1 #include <Windows.h>
2 #include <tchar.h>
3 #include <math.h>
4 typedef struct Time
5 {
6 int hour, min, sec;
7 }TimeStructure;
8 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
9 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
10 void AdjustTime(TimeStructure *x);
11 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
12 {
13 MSG msg;
14 if (!InitWindowClass(hInstance, nCmdShow))
15 {
16 MessageBox(NULL, L"創建窗口失敗!", _T("創建窗口"), NULL);
17 return 1;
18 }
19 while (GetMessage(&msg, NULL, 0, 0))
20 {
21 TranslateMessage(&msg);
22 DispatchMessage(&msg);
23 }
24 return(int)msg.wParam;
25 }
26 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
27 {
28 HDC hDC;
29 PAINTSTRUCT ps;
30 HBRUSH hBrush;
31 HPEN hPen;
32 RECT clientRect;
33 static TimeStructure x;
34 float sita = 0;
35 int xOrg, yOrg, rSec, rMin, rHour, rClock, xBegin, xEnd, yBegin, yEnd;
36 switch (message)
37 {
38 case WM_CREATE: //創建窗口時,響應的消息
39 SetTimer(hWnd, 9999, 1000, NULL); //設置定時器
40 break;
41 case WM_PAINT:
42 x.sec++;
43 AdjustTime(&x);
44 hDC = BeginPaint(hWnd, &ps);
45 GetClientRect(hWnd, &clientRect); //獲得用戶區的尺寸
46 hPen = (HPEN)GetStockObject(BLACK_PEN); //設置畫筆為系統預定義的黑色畫筆
47 hBrush = CreateSolidBrush(RGB(255, 220, 220)); //創建粉紅色的單色畫刷
48 SelectObject(hDC, hPen); //選擇畫筆
49 SelectObject(hDC, hBrush); //選擇畫刷
50 xOrg = (clientRect.left + clientRect.right) / 2;
51 yOrg = (clientRect.top + clientRect.bottom) / 2; //計算屏幕中心的坐標,它也是時鐘的中心
52 rClock = min(xOrg, yOrg) - 50; //鐘表的半徑
53 rSec = rClock * 6 / 7; //秒針的半徑
54 rMin = rClock * 5 / 6; //分針的半徑
55 rHour = rClock * 2 / 3; //時針的半徑
56 Ellipse(hDC, xOrg - rClock, yOrg - rClock, xOrg + rClock, yOrg + rClock);//繪制表面圓
57 for (int i = 0; i < 60; i++) //繪制表面的刻度
58 {
59 if (i % 5) //繪制表面表面的整點刻度
60 {
61 hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
62 SelectObject(hDC, hPen);
63 xBegin = xOrg + rClock*sin(2 * 3.1415926*i / 60);
64 yBegin = yOrg + rClock*cos(2 * 3.1415926*i / 60);
65 MoveToEx(hDC, xBegin, yBegin, NULL);
66 xEnd = xOrg + (rClock - 20)*sin(2 * 3.1415926*i / 60);
67 yEnd = yOrg + (rClock - 20)*cos(2 * 3.1415926*i / 60);
68
69 }
70 else //繪制表面表面的非整點刻度
71 {
72 hPen = CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
73 SelectObject(hDC, hPen);
74 xBegin = xOrg + rClock*sin(2 * 3.1415926*i / 60);
75 yBegin = yOrg + rClock*cos(2 * 3.1415926*i / 60);
76 MoveToEx(hDC, xBegin, yBegin, NULL);
77 xEnd = xOrg + (rClock - 25)*sin(2 * 3.1415926*i / 60);
78 yEnd = yOrg + (rClock - 25)*cos(2 * 3.1415926*i / 60);
79 }
80 LineTo(hDC, xEnd, yEnd);
81 DeleteObject(hPen);
82 }
83 hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
84 SelectObject(hDC, hPen);
85 sita = 2 * 3.1415926*x.sec / 60;
86 xBegin = xOrg + (int)(rSec*sin(sita));
87 yBegin = yOrg - (int)(rSec*cos(sita)); //秒針的起點,它的位置在秒針的最末端
88 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8);
89 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);//秒針的終點,它的位置在秒針的反方向的長度為秒針的1/8
90 MoveToEx(hDC, xBegin, yBegin, NULL);
91 LineTo(hDC, xEnd, yEnd); //繪制秒針
92 hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0));
93 SelectObject(hDC, hPen);
94 sita = 2 * 3.1415926*x.min / 60;
95 xBegin = xOrg + (int)(rMin*sin(sita));
96 yBegin = yOrg - (int)(rMin*cos(sita)); //分針的起點
97 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8);
98 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);//分針的終點
99 MoveToEx(hDC, xBegin, yBegin, NULL);
100 LineTo(hDC, xEnd, yEnd); //繪制分針
101 hPen = CreatePen(PS_SOLID, 10, RGB(0, 0, 0));
102 SelectObject(hDC, hPen);
103 sita = 2 * 3.1415926*x.hour / 12;
104 xBegin = xOrg + (int)(rHour*sin(sita));
105 yBegin = yOrg - (int)(rHour*cos(sita));
106 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8);
107 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);
108 MoveToEx(hDC, xBegin, yBegin, NULL);
109 LineTo(hDC, xEnd, yEnd); //繪制時針
110 DeleteObject(hPen);
111 DeleteObject(hBrush);
112 EndPaint(hWnd, &ps); //結束繪圖
113 break;
114 case WM_TIMER: //響應定時器發出的定時消息
115 if (wParam == 9999) //判斷是否是設置的定時器發出的消息
116 InvalidateRect(hWnd, NULL, true); //刷新屏幕
117 break;
118 case WM_SIZE: //窗口尺寸改變時,刷新窗口
119 InvalidateRect(hWnd, NULL, true);
120 break;
121 case WM_DESTROY:
122 PostQuitMessage(0); //調用PostQuitMessage發出WM_QUIT消息
123 break;
124 default:
125 return DefWindowProc(hWnd, message, wParam, lParam); //默認時采用系統消息默認處理函數
126 break;
127 }
128 return 0;
129 }
130 void AdjustTime(TimeStructure *x)
131 {
132 if (x->sec == 60)
133 {
134 x->sec = 0;
135 x->min++;
136 if (x->min == 60)
137 {
138 x->min = 0;
139 x->hour++;
140 if (x->hour == 12)
141 x->hour = 0;
142 }
143 }
144 }
145 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow)
146 {
147 WNDCLASSEX wcex;
148 HWND hWnd;
149 TCHAR szWindowClass[] = L"窗口示例";
150 TCHAR szTitle[] = L"模擬時鐘";
151 wcex.cbSize = sizeof(WNDCLASSEX);
152 wcex.style = 0;
153 wcex.lpfnWndProc = WndProc;
154 wcex.cbClsExtra = 0;
155 wcex.cbWndExtra = 0;
156 wcex.hInstance = hInstance;
157 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
158 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
159 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
160 wcex.lpszMenuName = NULL;
161 wcex.lpszClassName = szWindowClass;
162 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
163 if (!RegisterClassEx(&wcex))
164 return FALSE;
165 hWnd = CreateWindow(
166 szWindowClass,
167 szTitle,
168 WS_OVERLAPPEDWINDOW,
169 CW_USEDEFAULT, CW_USEDEFAULT,
170 CW_USEDEFAULT, CW_USEDEFAULT,
171 NULL,
172 NULL,
173 hInstance,
174 NULL
175 );
176 if (!hWnd)
177 return FALSE;
178 ShowWindow(hWnd, nCmdShow);
179 UpdateWindow(hWnd);
180 return TRUE;
181 }