alien_fx_fiend

Analog Clock Win32 *FINAL RELEASE !*

Oct 22nd, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.29 KB | Source Code | 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.  
  43.     // Get screen dimensions
  44.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  45.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  46.  
  47.     // Calculate centered position
  48.     int windowWidth = rect.right - rect.left;
  49.     int windowHeight = rect.bottom - rect.top;
  50.     int xPos = (screenWidth - windowWidth) / 2;
  51.     int yPos = (screenHeight - windowHeight) / 2;
  52.  
  53.     hwnd = CreateWindow(szAppName, TEXT("Time is Meaningless The Insurgency is Live & Well (F1=About)"),
  54.         WS_OVERLAPPEDWINDOW, xPos, yPos,
  55.         windowWidth, windowHeight,
  56.         NULL, NULL, hInstance, NULL);
  57.  
  58.     ShowWindow(hwnd, iCmdShow);
  59.     UpdateWindow(hwnd);
  60.  
  61.     while (GetMessage(&msg, NULL, 0, 0)) {
  62.         TranslateMessage(&msg);
  63.         DispatchMessage(&msg);
  64.     }
  65.     return (int)msg.wParam;
  66. }
  67.  
  68. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  69.     static HWND hwndStatus;
  70.     static HBITMAP hBitmap;
  71.     static int cxClient, cyClient;
  72.     HDC hdc, memDC;
  73.     PAINTSTRUCT ps;
  74.     SYSTEMTIME st;
  75.     RECT rect;
  76.     int centerX, centerY;
  77.  
  78.     // Moved the declaration outside the switch
  79.     int statwidths[] = { -1 };
  80.  
  81.     switch (message) {
  82.     case WM_CREATE:
  83.         hwndStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
  84.             WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
  85.             hwnd, (HMENU)ID_STATUSBAR, GetModuleHandle(NULL), NULL);
  86.  
  87.         SendMessage(hwndStatus, SB_SETPARTS, 1, (LPARAM)statwidths);
  88.         SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)TEXT(""));
  89.         SetWindowSubclass(hwndStatus, StatusBarProc, 0, 0);
  90.         SetTimer(hwnd, ID_TIMER, 1000, NULL);
  91.         return 0;
  92.     case WM_KEYDOWN:
  93.         if (wParam == VK_ESCAPE) {
  94.             PostQuitMessage(0);
  95.             return 0;
  96.         }
  97.         // Add this new code block
  98.         else if (wParam == VK_F1) {
  99.             MessageBox(hwnd, TEXT("Analog Clock (incorporates SubClassing Controls) Programmed in C++ Win32 API (278 lines of code) by Entisoft Software (c) Evans Thorpemorton"), TEXT("Information"), MB_OK | MB_ICONINFORMATION);
  100.             return 0;
  101.         }
  102.         break;
  103.     case WM_SIZE:
  104.         cxClient = LOWORD(lParam);
  105.         cyClient = HIWORD(lParam);
  106.         if (hBitmap) {
  107.             DeleteObject(hBitmap);
  108.         }
  109.         hdc = GetDC(hwnd);
  110.         hBitmap = CreateCompatibleBitmap(hdc, cxClient, cyClient);
  111.         ReleaseDC(hwnd, hdc);
  112.         SendMessage(hwndStatus, WM_SIZE, 0, 0);
  113.         return 0;
  114.     case WM_PAINT:
  115.         hdc = BeginPaint(hwnd, &ps);
  116.         memDC = CreateCompatibleDC(hdc);
  117.         SelectObject(memDC, hBitmap);
  118.         GetClientRect(hwnd, &rect);
  119.         centerX = (rect.right - rect.left) / 2;
  120.         centerY = ((rect.bottom - rect.top) - 20) / 2;
  121.         FillRect(memDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
  122.         DrawClock(memDC, centerX, centerY, min(centerX, centerY) - CLOCK_MARGIN);
  123.         BitBlt(hdc, 0, 0, cxClient, cyClient, memDC, 0, 0, SRCCOPY);
  124.         DeleteDC(memDC);
  125.         EndPaint(hwnd, &ps);
  126.         return 0;
  127.     case WM_TIMER:
  128.         GetLocalTime(&st);
  129.         UpdateStatusBar(hwndStatus, st);
  130.         InvalidateRect(hwnd, NULL, FALSE);
  131.         return 0;
  132.     case WM_DESTROY:
  133.         KillTimer(hwnd, ID_TIMER);
  134.         if (hBitmap) DeleteObject(hBitmap);
  135.         PostQuitMessage(0);
  136.         return 0;
  137.     }
  138.     return DefWindowProc(hwnd, message, wParam, lParam);
  139. }
  140.  
  141. void DrawClock(HDC hdc, int x, int y, int r) {
  142.     SYSTEMTIME st;
  143.     GetLocalTime(&st);
  144.     HDC memDC = CreateCompatibleDC(hdc);
  145.     HBITMAP hBitmap = CreateCompatibleBitmap(hdc, 2 * r, 2 * r);
  146.     SelectObject(memDC, hBitmap);
  147.     TRIVERTEX vertex[2];
  148.     vertex[0].x = 0;
  149.     vertex[0].y = 0;
  150.     vertex[0].Red = 0xc000;
  151.     vertex[0].Green = 0xc000;
  152.     vertex[0].Blue = 0xc000;
  153.     vertex[0].Alpha = 0x0000;
  154.     vertex[1].x = 2 * r;
  155.     vertex[1].y = 2 * r;
  156.     vertex[1].Red = 0x4000;
  157.     vertex[1].Green = 0x4000;
  158.     vertex[1].Blue = 0x4000;
  159.     vertex[1].Alpha = 0x0000;
  160.     GRADIENT_RECT gRect = { 0, 1 };
  161.     GradientFill(memDC, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_H);
  162.     HPEN hPen = CreatePen(PS_SOLID, 8, RGB(128, 128, 128));
  163.     HPEN hOldPen = (HPEN)SelectObject(memDC, hPen);
  164.     Ellipse(memDC, 0, 0, 2 * r, 2 * r);
  165.     BitBlt(hdc, x - r, y - r, 2 * r, 2 * r, memDC, 0, 0, SRCCOPY);
  166.     SelectObject(memDC, hOldPen);
  167.     DeleteObject(hPen);
  168.     DeleteObject(hBitmap);
  169.     DeleteDC(memDC);
  170.     DrawHand(hdc, x, y, st.wSecond * 6, r - 30, RGB(255, 0, 0));
  171.     DrawHand(hdc, x, y, (st.wMinute * 6) + (st.wSecond / 10.0), r - 60, RGB(0, 0, 0));
  172.     DrawHand(hdc, x, y, (st.wHour * 30) + (st.wMinute / 2.0), r - 90, RGB(0, 0, 255));
  173.     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"));
  174.     HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
  175.     SetTextColor(hdc, RGB(112, 128, 144));
  176.     SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
  177.     SetBkMode(hdc, TRANSPARENT);
  178.     for (int i = 1; i <= 12; i++) {
  179.         double angle = (i * 30 - 90) * (3.14159265358979323846 / 180);
  180.         int numX = x + (int)(cos(angle) * (r - 25));
  181.         int numY = y + (int)(sin(angle) * (r - 25));
  182.         WCHAR numStr[3];
  183.         wsprintf(numStr, L"%d", i);
  184.         TextOut(hdc, numX, numY, numStr, lstrlen(numStr));
  185.     }
  186.     SelectObject(hdc, hOldFont);
  187.     DeleteObject(hFont);
  188.     for (int i = 0; i < 60; i++) {
  189.         if (i % 5 == 0) {
  190.             DrawHand(hdc, x, y, i * 6, r - 10, RGB(0, 0, 0), false);
  191.         }
  192.         else {
  193.             DrawHand(hdc, x, y, i * 6, r - 5, RGB(0, 0, 0), false);
  194.         }
  195.     }
  196. }
  197.  
  198. void DrawHand(HDC hdc, int x, int y, double angle, int length, COLORREF color, bool drawLineToCenter) {
  199.     double radian = (angle - 90) * (3.14159265358979323846 / 180);
  200.     int endX = x + (int)(cos(radian) * length);
  201.     int endY = y + (int)(sin(radian) * length);
  202.     HPEN hPen = CreatePen(PS_SOLID, 2, color);
  203.     HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  204.     if (drawLineToCenter) {
  205.         MoveToEx(hdc, x, y, NULL);
  206.         LineTo(hdc, endX, endY);
  207.     }
  208.     else {
  209.         MoveToEx(hdc, endX, endY, NULL);
  210.         LineTo(hdc, endX + (int)(cos(radian) * 5), endY + (int)(sin(radian) * 5));
  211.     }
  212.     SelectObject(hdc, hOldPen);
  213.     DeleteObject(hPen);
  214. }
  215.  
  216. void UpdateStatusBar(HWND hwndStatus, SYSTEMTIME st) {
  217.     TCHAR szStatus[256];
  218.     TCHAR szDayOfWeek[32], szMonth[32];
  219.     TCHAR szSuffix[3];
  220.     int weekNumber;
  221.  
  222.     GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("dddd"), szDayOfWeek, sizeof(szDayOfWeek) / sizeof(TCHAR));
  223.     GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("MMMM"), szMonth, sizeof(szMonth) / sizeof(TCHAR));
  224.  
  225.     SYSTEMTIME newYearDay = { st.wYear, 1, 0, 1 };
  226.     FILETIME ftNewYearDay, ftCurrentDay;
  227.     SystemTimeToFileTime(&newYearDay, &ftNewYearDay);
  228.     SystemTimeToFileTime(&st, &ftCurrentDay);
  229.     ULARGE_INTEGER uliNewYearDay, uliCurrentDay;
  230.     uliNewYearDay.LowPart = ftNewYearDay.dwLowDateTime;
  231.     uliNewYearDay.HighPart = ftNewYearDay.dwHighDateTime;
  232.     uliCurrentDay.LowPart = ftCurrentDay.dwLowDateTime;
  233.     uliCurrentDay.HighPart = ftCurrentDay.dwHighDateTime;
  234.     weekNumber = (int)(((uliCurrentDay.QuadPart - uliNewYearDay.QuadPart) / (7 * 24 * 60 * 60 * 10000000ULL)) + 1);
  235.  
  236.     if (st.wDay == 1 || st.wDay == 21 || st.wDay == 31)
  237.         _tcscpy_s(szSuffix, _T("st"));
  238.     else if (st.wDay == 2 || st.wDay == 22)
  239.         _tcscpy_s(szSuffix, _T("nd"));
  240.     else if (st.wDay == 3 || st.wDay == 23)
  241.         _tcscpy_s(szSuffix, _T("rd"));
  242.     else
  243.         _tcscpy_s(szSuffix, _T("th"));
  244.  
  245.     StringCbPrintf(szStatus, sizeof(szStatus),
  246.         TEXT("%s %d%s %s %d #%d %d/%02d/%04d %02d:%02d:%02d %s"),
  247.         szDayOfWeek, st.wDay, szSuffix, szMonth, st.wYear, weekNumber,
  248.         st.wDay, st.wMonth, st.wYear,
  249.         (st.wHour == 0 || st.wHour == 12) ? 12 : st.wHour % 12,
  250.         st.wMinute, st.wSecond,
  251.         st.wHour < 12 ? TEXT("AM") : TEXT("PM"));
  252.  
  253.     SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)szStatus);
  254.     InvalidateRect(hwndStatus, NULL, TRUE);
  255. }
  256.  
  257. LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
  258.     if (uMsg == WM_PAINT) {
  259.         PAINTSTRUCT ps;
  260.         HDC hdc = BeginPaint(hWnd, &ps);
  261.  
  262.         RECT rc;
  263.         GetClientRect(hWnd, &rc);
  264.  
  265.         TCHAR szText[256];
  266.         int len = SendMessage(hWnd, SB_GETTEXT, 0, (LPARAM)szText);
  267.  
  268.         SetBkMode(hdc, TRANSPARENT);
  269.         SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  270.  
  271.         DrawText(hdc, szText, len, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  272.  
  273.         EndPaint(hWnd, &ps);
  274.         return 0;
  275.     }
  276.  
  277.     return DefSubclassProc(hWnd, uMsg, wParam, lParam);
  278. }
Add Comment
Please, Sign In to add comment