reloj analógico en Win32

 


#include <Windows.h> #include <math.h> #include <tchar.h> #include "resource.h" #define PI 3.1415926 BOOLEANA 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 "No se pudo crear Windows", L "info", NULL); volver 1; } While (GetMessage (y msg, NULL, 0,0)) { TranslateMessage (y msg); DispatchMessage (y msg); } Retorno (int) msg. wParam; } BOOLEANA 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.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; //注册窗口类 si (RegisterClassEx (y 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); si (CVent!) return false; ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return TRUE; } LRESULT CALLBACK WndProc (HWND hWnd, mensaje UINT, WPARAM wParam, LPARAM lParam) { HDC HDC; Ps PAINTSTRUCT; HBRUSH hBrush; HPEN HPEN; RECT clientRect; SYSTEMTIME X; = 0 SITA un flotador; Doble RSEC, Rmin, rHour, temHour, temMin, xorg, Yorg, rclock, ptLong, xBegin, xFin, yBegin, yEnd; // Mensaje Distribución Switch (Mensaje) { caso de que el WM_CREATE: la SetTimer (el hWnd, 9999, 1000, NULL); PAUSA; caso la WM_PAINT: HDC = la BeginPaint (el hWnd, y PS); la GetClientRect (el hWnd, y ClientRect); // obtener el tamaño de la ventana HPEN = (HPEN) la GetStockObject (BLACK_PEN); hBrush = CreateSolidBrush (el RGB (255, 220 , 220)); la SelectObject (HDC, HPEN); la SelectObject (HDC, hBrush); // coordinar reloj de puntos marcar xorg = (clientRect.left clientRect.right +) / 2; Yorg = (+ clientRect.top clientRect.bottom ) / 2; // dial, segundos, minutos, horas radio rclock = min (XOrg, yorg) * 4/5; RSEC = rclock * 6/7; Rmin = rclock * 5/6; rHour = rclock * 2/3; //画圆盘 Elipse (HDC, int (XOrg - rclock), int (yorg - rclock), int (XOrg + rclock), int (yorg + rclock)); //画刻度 for (int i = 0; i <60; i ++) { si (i% 5) { HPEN = CreatePen (PS_SOLID, 2, RGB (255, 0, 0)); ptLong = rclock * 9/10; } Más { HPEN = CreatePen (PS_SOLID, 5, RGB (255, 0, 0)); ptLong = rclock * 7/8; } SelectObject (HDC, HPEN); //刻度起止点 xBegin = XOrg + rclock * sin (2 * pi * i / 60); yBegin = yorg + rclock * cos (2 * pi * i / 60); xFin = 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) xFin, (int) yEnd); DeleteObject (HPEN); } GetLocalTime (y x); //画秒针 HPEN = CreatePen (PS_SOLID, 2, RGB (255, 0, 0)); SelectObject (HDC, HPEN); sita = flotador (2 * pi * x.wSecond / 60); //秒针起止点 xBegin = XOrg + (int) (RSEC * sin (sita)); yBegin = yorg - (int) (RSEC * cos (sita)); xFin = 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) xFin, (int) yEnd); //画分针 HPEN = CreatePen (PS_SOLID, 5, RGB (0, 0, 0)); SelectObject (HDC, HPEN); sita = flotador (2 * pi * x.wMinute / 60); //分针起止点 xBegin = XOrg + (int) (Rmin * sin (sita)); yBegin = yorg - (int) (Rmin cos * (sita)); xFin = 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) xFin, (int) yEnd); //画时针 HPEN = CreatePen (PS_SOLID, 10, RGB (0, 0, 0)); SelectObject (HDC, HPEN); / * si (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; } el otro IF (x.wMinute> 36 && x.wMinute <= 48) { SITA la PI * = 2 * (x.wHour + (48 / 60)) / 12 es; } la eLSE IF (x.wMinute> 48 && x.wMinute <= 59) { SITA la PI * = 2 * (x.wHour + (doble) (x.wMinute / 60)) / 12 es; } else { SITA = 2 * * x.wHour la PI / 12 es; } * /
temMin = (doble) (x.wMinute) / 60; // convertir los puntos a las horas decimales, es decir, 30 minutos -> 0,5 horas temHour = x.wHour + temMin; // plano decimal tiempo la mano horas, 1: 30-> 1,5 horas


// set así que el tiempo horas 3:30 no es 3, sustituido por 3.5, y de esa manera la esfera del reloj estará en el medio de la escala de dos horas
// no se refiere simplemente a un invariante escala horas 3 <_>;

SITA = un flotador (2 * * temHour la PI / 12 es); // iniciar puntos horas xBegin = XOrg + (doble) (rHour * sen (Sita)); yBegin = yorg - (doble) (rHour * cos (sita)); xFin = xorg + (doble) (rclock * sin (sita + PI ) / 8); yEnd = yorg - (doble) (rclock * cos (sita + PI) / 8); MoveToEx (HDC, (int) xBegin, (int) yBegin, NULL); LineTo (HDC, (int) xFin, (int) yEnd); DeleteObject (HPEN); DeleteObject (hBrush); EndPaint (hWnd, y ps); descanso; caso WM_TIMER: si (wParam == 9999) InvalidateRect (hWnd, NULL, true); descanso; caso WM_SIZE: InvalidateRect (hWnd, NULL, TRUE); descanso; caso WM_DESTROY: PostQuitMessage (0); descanso; por defecto: el retorno :: DefWindowProc (CVent, mensaje, wParam, lParam); } Return 0; }

  

copia en línea de, por blanca acaba de aprender, la única copia, pero una hora antes de la hora en línea este salto, no se ve bien, tengo que mejorar la próxima.

El atractivo principal es la hora en que el tiempo (03:30) en la representación decimal de 3,5 está bien, que es simple, he tratado durante varias horas.

Supongo que te gusta

Origin www.cnblogs.com/greenleaf1976/p/12635641.html
Recomendado
Clasificación