#include <Windows.h> #include <math.h> #include <tchar.h> BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MSG msg; if(!InitWindowClass(hInstance, nCmdShow)){ MessageBox(NULL, L"Failed to Create Windows", L"InFo", NULL); return 1; } while(GetMessage(&msg, NULL, 0, 0)){ TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow){ WNDCLASSEX wcex; HWND hWnd; TCHAR szWindowsClass[] = L"窗口示例"; TCHAR szTitle[] = L"模拟时钟"; wcex.cbClsExtra = 0; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.lpfnWndProc = WndProc; wcex.lpszClassName = szWindowsClass; wcex.lpszMenuName = NULL; wcex.style = 0; if(!RegisterClassEx(&wcex)){ return FALSE; } hWnd = CreateWindow(szWindowsClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if(!hWnd){ return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hDC; PAINTSTRUCT ps; HBRUSH hBrush; HPEN hPen; RECT clientRect; SYSTEMTIME x; float sita = 0; int xOrg, yOrg, rSec, rMin, rHour, rClock, ptLong, xBegin, xEnd, yBegin, yEnd; switch(message){ case WM_CREATE: SetTimer(hWnd, 9999, 1000, NULL); // 发送消息 break; case WM_PAINT: hDC = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &clientRect); // 获取窗口大小 hPen = (HPEN)GetStockObject(BLACK_PEN); hBrush = CreateSolidBrush(RGB(255, 220, 220)); SelectObject(hDC, hPen); SelectObject(hDC, hBrush); xOrg = (clientRect.left + clientRect.right) / 2; // 时钟表盘圆点坐标 yOrg = (clientRect.bottom + clientRect.top) / 2; rClock = min(xOrg, yOrg) * 4 / 5; // 表盘,秒针,分针,时针半径 rSec = rClock * 6 / 7; rMin = rClock * 5 / 6; rHour = rClock * 2 / 3; Ellipse(hDC, xOrg - rClock, yOrg - rClock, xOrg + rClock, yOrg + rClock); // 画圆盘 for(int i = 0; i < 60; i ++){ // 画刻度 if(i % 5){ hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); ptLong = rClock * 9 / 10; } else{ hPen = CreatePen(PS_SOLID, 5, RGB(255, 0, 0)); ptLong = rClock * 7 / 8; } SelectObject(hDC, hPen); xBegin = xOrg + rClock * sin(2 * 3.1415926 * i / 60); // 刻度起止点 yBegin = yOrg + rClock * cos(2 * 3.1415926 * i / 60); xEnd = xOrg + ptLong * sin(2 * 3.1415926 * i / 60); yEnd = yOrg + ptLong * cos(2 * 3.1415926 * i / 60); MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd); DeleteObject(hPen); } GetLocalTime(&x); hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // 秒针 SelectObject(hDC, hPen); sita = 2 * 3.1415926 * x.wSecond / 60; xBegin = xOrg + (int)(rSec * sin(sita)); // 起止点 yBegin = yOrg - (int)(rSec * cos(sita)); xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926) / 8); yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926) / 8); MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd); hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0)); // 分针 SelectObject(hDC, hPen); sita = 2 * 3.1415926 * x.wMinute / 60; xBegin = xOrg + (int)(rMin * sin(sita)); yBegin = yOrg - (int)(rMin * cos(sita)); xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926) / 8); yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926) / 8); MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd); hPen = CreatePen(PS_SOLID, 10, RGB(0, 0, 0)); // 时针 SelectObject(hDC, hPen); sita = 2 * 3.1415926 * x.wHour / 12; xBegin = xOrg + (int)(rHour * sin(sita)); yBegin = yOrg - (int)(rHour * cos(sita)); xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926) / 8); yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926) / 8); MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd); DeleteObject(hPen); DeleteObject(hBrush); EndPaint(hWnd, &ps); break; case WM_TIMER: if(wParam == 9999){ InvalidateRect(hWnd, NULL, true); } break; case WM_SIZE: InvalidateRect(hWnd, NULL, TRUE); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }