Analog clock under WIN32

 


#include <Windows.h> #include <math.h> #include <tchar.h> #include "resource.h" #define PI 3.1415926 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 "sample window"; TCHAR szTitle [] = L "analog clock"; wcex.cbClsExtra = 0; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcex.hInstance = hInstance; wcex.hIconSm = LoadCursor(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1)); wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpfnWndProc = WndProc; wcex.lpszClassName = szWindowsClass; wcex.lpszMenuName = NULL; wcex.style = CS_HREDRAW | CS_VREDRAW; // Register the window class if (!RegisterClassEx(&wcex)) { return FALSE; } //hWnd = CreateWindowEx(0,szWindowsClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); hWnd = CreateWindowEx(0,szWindowsClass, szTitle, WS_OVERLAPPEDWINDOW, 200, 200, 600, 400, 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; site float = 0; double rSec, rMin, rHour,temHour,temMin, xOrg, yOrg, rClock, ptLong, xBegin, xEnd, yBegin, yEnd; // Message Distribution switch (message) { case WM_CREATE: SetTimer(hWnd, 9999, 1000, NULL); break; case WM_PAINT: hDc = BeginPaint(hWnd, &ps); GetClientRect (hWnd, & clientRect); // Get the window size hPen = (HPEN)GetStockObject(BLACK_PEN); hBrush = CreateSolidBrush(RGB(255, 220, 220)); SelectObject(hDc, hPen); SelectObject(hDc, hBrush); // clock dial dot coordinates xOrg = (clientRect.left + clientRect.right) / 2; yOrg = (clientRect.bottom + clientRect.top) / 2; // dial, seconds, minutes, hour radius rClock = min (xOrg, yOrg) * 4/5; rSec = rClock * 6 / 7; rMin = rClock * 5 / 6; rHour = rClock * 2 / 3; // draw disc Ellipse(hDc, int(xOrg - rClock), int(yOrg - rClock), int(xOrg + rClock), int(yOrg + rClock)); // scale painting 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); // start and end point scale xBegin = xOrg + rClock * sin(2 * PI*i / 60); yBegin = yOrg + rClock * cos (2 * PI * i / 60); xEnd = xOrg + ptLong * sin(2 * PI*i / 60); yEnd = yOrg + ptLong * cos (2 * PI * i / 60); MoveToEx(hDc, (int)xBegin,(int)yBegin, NULL); LineTo(hDc, (int)xEnd, (int)yEnd); DeleteObject(hPen); } GetLocalTime(&x); // draw the second hand hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); SelectObject(hDc, hPen); site = float (2 * PI * x.wSecond / 60); // seconds starting and ending points xBegin = xOrg + (int)(rSec*sin(sita)); yBegin yOrg = - (int) (SR * cos (Sita)); xEnd = xOrg + (int)(rClock*sin(sita + PI) / 8); yEnd = yOrg - (int) (rClock * cos (sita + PI) / 8); MoveToEx(hDc, (int)xBegin, (int)yBegin, NULL); LineTo(hDc, (int)xEnd, (int)yEnd); // draw the minute hand hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0)); SelectObject(hDc, hPen); sita = float(2 * PI*x.wMinute / 60); // minute starting and ending points xBegin = xOrg + (int)(rMin*sin(sita)); yBegin yOrg = - (int) (Rmin * cos (Sita)); xEnd = xOrg + (int)(rClock*sin(sita + PI) / 8); yEnd = yOrg - (int) (rClock * cos (sita + PI) / 8); MoveToEx(hDc, (int)xBegin, (int)yBegin, NULL); LineTo(hDc, (int)xEnd, (int)yEnd); // draw the hour hPen = CreatePen(PS_SOLID, 10, RGB(0, 0, 0)); SelectObject(hDc, hPen); /*if (x.wMinute >= 12 && x.wMinute<=24) sita = 2 * PI*(x.wHour +(12/60)) / 12; else if(x.wMinute>24 && x.wMinute<=36) { sita = 2 * PI*(x.wHour + (24/60)) / 12; } else if(x.wMinute>36 && x.wMinute<=48) { sita = 2 * PI*(x.wHour + (48/60)) / 12; } else if (x.wMinute > 48 && x.wMinute<=59) { sita = 2 * PI*(x.wHour + (double)(x.wMinute/60)) / 12; } else { sita = 2 * PI*x.wHour / 12; }*/
temMin = (double) (x.wMinute) / 60; // convert the points to the decimal hours, i.e. 30 minutes -> 0.5 hours temHour = x.wHour + temMin; // Plane decimal time the hour hand, 1: 30-> 1.5 hours


// so hour setting time 3:30 is not 3, replaced by 3.5, and in that way the clock intermediate scale on the dial will be in two hours
// refers not just to an hour scale invariant 3 <_>;

site = float (2 * PI * temHour / 12); // hour starting and ending points xBegin = xOrg + (double)(rHour*sin(sita)); yBegin = yOrg - (double)(rHour*cos(sita)); xEnd = xOrg + (double)(rClock*sin(sita + PI) / 8); yEnd = yOrg - (double)(rClock*cos(sita + PI) / 8); MoveToEx(hDc, (int)xBegin, (int)yBegin, NULL); LineTo(hDc, (int)xEnd, (int)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; }

  

Online copy of, for white just learned, the only copy, but an hour before the hour online this jump, does not look good, I have to improve the next.

The main draw is the hour when the time (3:30) into the decimal representation of 3.5 is OK, that is simple, I have tried for several hours.

Guess you like

Origin www.cnblogs.com/greenleaf1976/p/12635641.html