Advertisement
alien_fx_fiend

Analog Clock Win32 (Center-Aligned) [*Newest FINAL*]

Jul 26th, 2024
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.48 KB | None | 0 0
  1. #include <windows.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <commctrl.h>
  6. #include <tchar.h>
  7. #include <strsafe.h>
  8. #pragma comment(lib, "Msimg32.lib")
  9. #pragma comment(lib, "comctl32.lib")
  10. #define ID_TIMER 1
  11. #define SIZE 425
  12. #define CLOCK_MARGIN 20
  13. #define ID_STATUSBAR 100
  14.  
  15. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  16. void DrawClock(HDC, int, int, int);
  17. void DrawHand(HDC, int, int, double, int, COLORREF, bool drawLineToCenter = true);
  18. void UpdateStatusBar(HWND, SYSTEMTIME);
  19. LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
  20.  
  21. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
  22.     static TCHAR szAppName[] = TEXT("My Clock");
  23.     HWND hwnd;
  24.     MSG msg;
  25.     WNDCLASS wndclass;
  26.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  27.     wndclass.lpfnWndProc = WndProc;
  28.     wndclass.cbClsExtra = 0;
  29.     wndclass.cbWndExtra = 0;
  30.     wndclass.hInstance = hInstance;
  31.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  32.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  33.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  34.     wndclass.lpszMenuName = NULL;
  35.     wndclass.lpszClassName = szAppName;
  36.     if (!RegisterClass(&wndclass)) {
  37.         MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR);
  38.         return 0;
  39.     }
  40.     RECT rect = { 0, 0, SIZE + 2 * CLOCK_MARGIN, SIZE + 2 * CLOCK_MARGIN + 30 };
  41.     AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
  42.     hwnd = CreateWindow(szAppName, TEXT("Time is Meaningless The Insurgency is Live & Well"),
  43.         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  44.         rect.right - rect.left, rect.bottom - rect.top,
  45.         NULL, NULL, hInstance, NULL);
  46.  
  47.     ShowWindow(hwnd, iCmdShow);
  48.     UpdateWindow(hwnd);
  49.  
  50.     while (GetMessage(&msg, NULL, 0, 0)) {
  51.         TranslateMessage(&msg);
  52.         DispatchMessage(&msg);
  53.     }
  54.     return (int)msg.wParam;
  55. }
  56.  
  57. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  58.     static HWND hwndStatus;
  59.     static HBITMAP hBitmap;
  60.     static int cxClient, cyClient;
  61.     HDC hdc, memDC;
  62.     PAINTSTRUCT ps;
  63.     SYSTEMTIME st;
  64.     RECT rect;
  65.     int centerX, centerY;
  66.  
  67.     // Moved the declaration outside the switch
  68.     int statwidths[] = { -1 };
  69.  
  70.     switch (message) {
  71.     case WM_CREATE:
  72.         hwndStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
  73.             WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
  74.             hwnd, (HMENU)ID_STATUSBAR, GetModuleHandle(NULL), NULL);
  75.  
  76.         SendMessage(hwndStatus, SB_SETPARTS, 1, (LPARAM)statwidths);
  77.         SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)TEXT(""));
  78.         SetWindowSubclass(hwndStatus, StatusBarProc, 0, 0);
  79.         SetTimer(hwnd, ID_TIMER, 1000, NULL);
  80.         return 0;
  81.     case WM_SIZE:
  82.         cxClient = LOWORD(lParam);
  83.         cyClient = HIWORD(lParam);
  84.         if (hBitmap) {
  85.             DeleteObject(hBitmap);
  86.         }
  87.         hdc = GetDC(hwnd);
  88.         hBitmap = CreateCompatibleBitmap(hdc, cxClient, cyClient);
  89.         ReleaseDC(hwnd, hdc);
  90.         SendMessage(hwndStatus, WM_SIZE, 0, 0);
  91.         return 0;
  92.     case WM_PAINT:
  93.         hdc = BeginPaint(hwnd, &ps);
  94.         memDC = CreateCompatibleDC(hdc);
  95.         SelectObject(memDC, hBitmap);
  96.         GetClientRect(hwnd, &rect);
  97.         centerX = (rect.right - rect.left) / 2;
  98.         centerY = ((rect.bottom - rect.top) - 20) / 2;
  99.         FillRect(memDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
  100.         DrawClock(memDC, centerX, centerY, min(centerX, centerY) - CLOCK_MARGIN);
  101.         BitBlt(hdc, 0, 0, cxClient, cyClient, memDC, 0, 0, SRCCOPY);
  102.         DeleteDC(memDC);
  103.         EndPaint(hwnd, &ps);
  104.         return 0;
  105.     case WM_TIMER:
  106.         GetLocalTime(&st);
  107.         UpdateStatusBar(hwndStatus, st);
  108.         InvalidateRect(hwnd, NULL, FALSE);
  109.         return 0;
  110.     case WM_DESTROY:
  111.         KillTimer(hwnd, ID_TIMER);
  112.         if (hBitmap) DeleteObject(hBitmap);
  113.         PostQuitMessage(0);
  114.         return 0;
  115.     }
  116.     return DefWindowProc(hwnd, message, wParam, lParam);
  117. }
  118.  
  119. void DrawClock(HDC hdc, int x, int y, int r) {
  120.     SYSTEMTIME st;
  121.     GetLocalTime(&st);
  122.     HDC memDC = CreateCompatibleDC(hdc);
  123.     HBITMAP hBitmap = CreateCompatibleBitmap(hdc, 2 * r, 2 * r);
  124.     SelectObject(memDC, hBitmap);
  125.     TRIVERTEX vertex[2];
  126.     vertex[0].x = 0;
  127.     vertex[0].y = 0;
  128.     vertex[0].Red = 0xc000;
  129.     vertex[0].Green = 0xc000;
  130.     vertex[0].Blue = 0xc000;
  131.     vertex[0].Alpha = 0x0000;
  132.     vertex[1].x = 2 * r;
  133.     vertex[1].y = 2 * r;
  134.     vertex[1].Red = 0x4000;
  135.     vertex[1].Green = 0x4000;
  136.     vertex[1].Blue = 0x4000;
  137.     vertex[1].Alpha = 0x0000;
  138.     GRADIENT_RECT gRect = { 0, 1 };
  139.     GradientFill(memDC, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_H);
  140.     HPEN hPen = CreatePen(PS_SOLID, 8, RGB(128, 128, 128));
  141.     HPEN hOldPen = (HPEN)SelectObject(memDC, hPen);
  142.     Ellipse(memDC, 0, 0, 2 * r, 2 * r);
  143.     BitBlt(hdc, x - r, y - r, 2 * r, 2 * r, memDC, 0, 0, SRCCOPY);
  144.     SelectObject(memDC, hOldPen);
  145.     DeleteObject(hPen);
  146.     DeleteObject(hBitmap);
  147.     DeleteDC(memDC);
  148.     DrawHand(hdc, x, y, st.wSecond * 6, r - 30, RGB(255, 0, 0));
  149.     DrawHand(hdc, x, y, (st.wMinute * 6) + (st.wSecond / 10.0), r - 60, RGB(0, 0, 0));
  150.     DrawHand(hdc, x, y, (st.wHour * 30) + (st.wMinute / 2.0), r - 90, RGB(0, 0, 255));
  151.     HFONT hFont = CreateFont(12, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, TEXT("Segoe UI"));
  152.     HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
  153.     SetTextColor(hdc, RGB(112, 128, 144));
  154.     SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
  155.     SetBkMode(hdc, TRANSPARENT);
  156.     for (int i = 1; i <= 12; i++) {
  157.         double angle = (i * 30 - 90) * (3.14159265358979323846 / 180);
  158.         int numX = x + (int)(cos(angle) * (r - 25));
  159.         int numY = y + (int)(sin(angle) * (r - 25));
  160.         WCHAR numStr[3];
  161.         wsprintf(numStr, L"%d", i);
  162.         TextOut(hdc, numX, numY, numStr, lstrlen(numStr));
  163.     }
  164.     SelectObject(hdc, hOldFont);
  165.     DeleteObject(hFont);
  166.     for (int i = 0; i < 60; i++) {
  167.         if (i % 5 == 0) {
  168.             DrawHand(hdc, x, y, i * 6, r - 10, RGB(0, 0, 0), false);
  169.         }
  170.         else {
  171.             DrawHand(hdc, x, y, i * 6, r - 5, RGB(0, 0, 0), false);
  172.         }
  173.     }
  174. }
  175.  
  176. void DrawHand(HDC hdc, int x, int y, double angle, int length, COLORREF color, bool drawLineToCenter) {
  177.     double radian = (angle - 90) * (3.14159265358979323846 / 180);
  178.     int endX = x + (int)(cos(radian) * length);
  179.     int endY = y + (int)(sin(radian) * length);
  180.     HPEN hPen = CreatePen(PS_SOLID, 2, color);
  181.     HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  182.     if (drawLineToCenter) {
  183.         MoveToEx(hdc, x, y, NULL);
  184.         LineTo(hdc, endX, endY);
  185.     }
  186.     else {
  187.         MoveToEx(hdc, endX, endY, NULL);
  188.         LineTo(hdc, endX + (int)(cos(radian) * 5), endY + (int)(sin(radian) * 5));
  189.     }
  190.     SelectObject(hdc, hOldPen);
  191.     DeleteObject(hPen);
  192. }
  193.  
  194. void UpdateStatusBar(HWND hwndStatus, SYSTEMTIME st) {
  195.     TCHAR szStatus[256];
  196.     TCHAR szDayOfWeek[32], szMonth[32];
  197.     TCHAR szSuffix[3];
  198.     int weekNumber;
  199.  
  200.     GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("dddd"), szDayOfWeek, sizeof(szDayOfWeek) / sizeof(TCHAR));
  201.     GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("MMMM"), szMonth, sizeof(szMonth) / sizeof(TCHAR));
  202.  
  203.     SYSTEMTIME newYearDay = { st.wYear, 1, 0, 1 };
  204.     FILETIME ftNewYearDay, ftCurrentDay;
  205.     SystemTimeToFileTime(&newYearDay, &ftNewYearDay);
  206.     SystemTimeToFileTime(&st, &ftCurrentDay);
  207.     ULARGE_INTEGER uliNewYearDay, uliCurrentDay;
  208.     uliNewYearDay.LowPart = ftNewYearDay.dwLowDateTime;
  209.     uliNewYearDay.HighPart = ftNewYearDay.dwHighDateTime;
  210.     uliCurrentDay.LowPart = ftCurrentDay.dwLowDateTime;
  211.     uliCurrentDay.HighPart = ftCurrentDay.dwHighDateTime;
  212.     weekNumber = (int)(((uliCurrentDay.QuadPart - uliNewYearDay.QuadPart) / (7 * 24 * 60 * 60 * 10000000ULL)) + 1);
  213.  
  214.     if (st.wDay == 1 || st.wDay == 21 || st.wDay == 31)
  215.         _tcscpy_s(szSuffix, _T("st"));
  216.     else if (st.wDay == 2 || st.wDay == 22)
  217.         _tcscpy_s(szSuffix, _T("nd"));
  218.     else if (st.wDay == 3 || st.wDay == 23)
  219.         _tcscpy_s(szSuffix, _T("rd"));
  220.     else
  221.         _tcscpy_s(szSuffix, _T("th"));
  222.  
  223.     StringCbPrintf(szStatus, sizeof(szStatus),
  224.         TEXT("%s %d%s %s %d #%d %d/%02d/%04d %02d:%02d:%02d %s"),
  225.         szDayOfWeek, st.wDay, szSuffix, szMonth, st.wYear, weekNumber,
  226.         st.wDay, st.wMonth, st.wYear,
  227.         (st.wHour == 0 || st.wHour == 12) ? 12 : st.wHour % 12,
  228.         st.wMinute, st.wSecond,
  229.         st.wHour < 12 ? TEXT("AM") : TEXT("PM"));
  230.  
  231.     SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)szStatus);
  232.     InvalidateRect(hwndStatus, NULL, TRUE);
  233. }
  234.  
  235. LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
  236.     if (uMsg == WM_PAINT) {
  237.         PAINTSTRUCT ps;
  238.         HDC hdc = BeginPaint(hWnd, &ps);
  239.  
  240.         RECT rc;
  241.         GetClientRect(hWnd, &rc);
  242.  
  243.         TCHAR szText[256];
  244.         int len = SendMessage(hWnd, SB_GETTEXT, 0, (LPARAM)szText);
  245.  
  246.         SetBkMode(hdc, TRANSPARENT);
  247.         SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  248.  
  249.         DrawText(hdc, szText, len, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  250.  
  251.         EndPaint(hWnd, &ps);
  252.         return 0;
  253.     }
  254.  
  255.     return DefSubclassProc(hWnd, uMsg, wParam, lParam);
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement