Clock(win32)
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <windows.h> 4 5 #include <math.h> 6 7 #include <time.h> 8 9 #pragma comment(lib, "user32.lib") 10 11 #pragma comment(lib, "gdi32.lib") 12 13 #pragma comment(linker, "/SUBSYSTEM:Windows") 14 15 16 17 void GetPos(double degree, int len, int* x, int* y) 18 19 { 20 21 *x = len * sin(degree); 22 23 *y = len * cos(degree); 24 25 } 26 27 28 29 void DrawClock(HWND hw, HDC h, int hour, int minute, int second) 30 31 { 32 33 HDC bufdc = CreateCompatibleDC(h); 34 35 HBITMAP buf = CreateCompatibleBitmap(bufdc, 200, 200); 36 37 RECT cr; 38 39 /* 创建一个白色的画刷 */ 40 HBRUSH bBg = CreateSolidBrush(RGB(255, 255, 255)); 41 42 GetClientRect(hw, &cr); 43 44 SelectObject(bufdc, bBg); 45 46 SelectObject(bufdc, buf); 47 48 FillRect(bufdc, &cr, bBg); 49 50 51 52 { 53 54 int sx, sy, ex, ey; 55 56 int i; 57 /* 这里应该是标记出表上的12个时间点 每个标记是一个小短线*/ 58 for(i = 0; i < 12; ++i) { 59 60 GetPos(2*3.14159/12*i, 85, &sx, &sy); 61 62 GetPos(2*3.14159/12*i, 95, &ex, &ey); 63 64 MoveToEx(bufdc, 100+sx, 100-sy, 0); 65 66 LineTo(bufdc, 100+ex, 100-ey); 67 68 } 69 70 } 71 72 73 /* 画出时针 分针 秒针 */ 74 { 75 76 int hx, hy, mx, my, sx, sy; 77 78 GetPos(2*3.14159 / 12 * (hour + (double)minute / 60 + (double)second / 3600), 50, &hx, &hy); 79 80 GetPos(2*3.14159 / 60 * (minute + (double)second / 60), 65, &mx, &my); 81 82 GetPos(2*3.14159 / 60 * second, 80, &sx, &sy); 83 84 MoveToEx(bufdc, 100, 100, NULL); 85 86 LineTo(bufdc, 100+hx, 100-hy); 87 88 MoveToEx(bufdc, 100, 100, NULL); 89 90 LineTo(bufdc, 100+mx, 100-my); 91 92 MoveToEx(bufdc, 100, 100, NULL); 93 94 LineTo(bufdc, 100+sx, 100-sy); 95 96 } 97 98 99 100 BitBlt(h, 0, 0, 200, 200, bufdc, 0, 0, SRCCOPY); 101 102 /* 释放分配的资源 */ 103 DeleteDC(bufdc); 104 105 DeleteObject(bBg); 106 107 DeleteObject(buf); 108 109 } 110 111 112 113 LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l) 114 115 { 116 117 switch(m) { 118 119 case WM_CREATE: 120 121 SetTimer(h, 1, 1000, 0); /* 设置时间间隔 每一秒作为一个时间间隔 */ 122 123 return 0; 124 125 case WM_ERASEBKGND: 126 127 return 1; 128 129 case WM_TIMER: 130 131 InvalidateRect(h, 0, FALSE); 132 133 return 0; 134 135 case WM_PAINT: { 136 137 PAINTSTRUCT ps; 138 139 HDC dc = BeginPaint(h, &ps); 140 141 { 142 143 time_t t; 144 145 struct tm* pst; 146 147 time(&t); 148 149 pst = localtime(&t); 150 /* 调用上面定义的函数 描绘出当前时钟 */ 151 DrawClock(h, dc, pst->tm_hour, pst->tm_min, pst->tm_sec); 152 153 } 154 155 EndPaint(h, &ps); 156 157 return 0; 158 159 } 160 161 case WM_DESTROY: 162 163 KillTimer(h, 1); /* 杀死定时器 */ 164 165 PostQuitMessage(0); 166 167 return 0; 168 169 default: 170 171 return DefWindowProc(h, m, w, l); 172 173 } 174 175 } 176 177 178 179 int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmd, INT nShow) 180 181 { 182 183 WNDCLASS wc; 184 185 ZeroMemory(&wc, sizeof(wc)); 186 187 wc.style = CS_HREDRAW | CS_VREDRAW; 188 189 wc.lpfnWndProc = WndProc; 190 191 wc.hInstance = hInst; 192 193 wc.hbrBackground = (HBRUSH) COLOR_WINDOW; 194 195 wc.lpszClassName = TEXT("MYCLOCK"); 196 197 198 199 if (RegisterClass(&wc) != 0) { 200 201 MSG m; 202 203 /* 创建窗口 */ 204 HWND hw = CreateWindow(TEXT("MYCLOCK"), TEXT("Clock"), WS_OVERLAPPEDWINDOW, 205 206 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, 0); 207 208 209 210 if (hw != NULL) { 211 212 RECT r; 213 214 GetWindowRect(hw, &r); 215 216 r.right = r.left + 200; 217 218 r.bottom = r.top + 200; 219 220 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE); 221 222 MoveWindow(hw, r.left, r.top, r.right - r.left, r.bottom - r.top, FALSE); 223 224 ShowWindow(hw, SW_SHOWNORMAL); /* 将窗口显示出来 */ 225 226 UpdateWindow(hw); 227 228 229 230 while( GetMessage(&m, 0, 0, 0) > 0) { 231 232 TranslateMessage(&m); 233 234 DispatchMessage(&m); 235 236 } 237 238 return 0; 239 240 } 241 242 return 1; 243 244 } else 245 246 return 1; 247 }