alien_fx_fiend

Analog Clock (Win32) Now Includes Calendar Popup Perusal (F2 Hotkey!!)

Mar 30th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.51 KB | Source Code | 0 0
  1. ==++ Here's the full source code for (file 1/1) "main.cpp"::: ++==
  2. ```main.cpp
  3. #include <windows.h>
  4. #include <gdiplus.h>
  5. #include <time.h>
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <commctrl.h>
  9. #include <tchar.h>
  10. #include <strsafe.h>
  11. #include "resource.h"  // Add this with your other includes
  12. #pragma comment(lib, "Msimg32.lib")
  13. #pragma comment(lib, "comctl32.lib")
  14. #pragma comment(lib, "gdiplus.lib")
  15. #define ID_TIMER 1
  16. #define SIZE 475
  17. #define CLOCK_MARGIN 20
  18. #define ID_STATUSBAR 100
  19. //SIZE 425
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21. void DrawClock(HDC, int, int, int);
  22. void DrawHand(HDC hdc, int x, int y, double angle, int length, COLORREF color, bool drawLineToCenter = true);
  23. void UpdateStatusBar(HWND, SYSTEMTIME);
  24. LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
  25. // Declare gdiplusToken as a global variable
  26. ULONG_PTR gdiplusToken;
  27.  
  28. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
  29.    // Add this to WinMain before the window creation:
  30.    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  31.    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  32.    static TCHAR szAppName[] = TEXT("My Clock");
  33.    HWND hwnd;
  34.    MSG msg;
  35.    WNDCLASS wndclass;
  36.    wndclass.style = CS_HREDRAW | CS_VREDRAW;
  37.    wndclass.lpfnWndProc = WndProc;
  38.    wndclass.cbClsExtra = 0;
  39.    wndclass.cbWndExtra = 0;
  40.    wndclass.hInstance = hInstance;
  41.    wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
  42.    //wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  43.    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  44.    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  45.    wndclass.lpszMenuName = NULL;
  46.    wndclass.lpszClassName = szAppName;
  47.    if (!RegisterClass(&wndclass)) {
  48.        MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR);
  49.        return 0;
  50.    }
  51.    RECT rect = { 0, 0, SIZE + 2 * CLOCK_MARGIN, SIZE + 2 * CLOCK_MARGIN + 30 };
  52.    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
  53.  
  54.    // Get screen dimensions
  55.    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  56.    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  57.  
  58.    // Calculate centered position
  59.    int windowWidth = rect.right - rect.left;
  60.    int windowHeight = rect.bottom - rect.top;
  61.    int xPos = (screenWidth - windowWidth) / 2;
  62.    int yPos = (screenHeight - windowHeight) / 2;
  63.  
  64.    hwnd = CreateWindowEx(WS_EX_TOPMOST, // Make the window always on top
  65.        szAppName, TEXT("Time is Meaningless The Insurgency is Live & Well (F1=About F2=Calendar)"),
  66.        WS_OVERLAPPEDWINDOW, xPos, yPos,
  67.        windowWidth, windowHeight,
  68.        NULL, NULL, hInstance, NULL);
  69.  
  70.    ShowWindow(hwnd, iCmdShow);
  71.    UpdateWindow(hwnd);
  72.  
  73.    while (GetMessage(&msg, NULL, 0, 0)) {
  74.        TranslateMessage(&msg);
  75.        DispatchMessage(&msg);
  76.    }
  77.    return (int)msg.wParam;
  78. }
  79.  
  80. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  81.    static HWND hwndStatus;
  82.    static HBITMAP hBitmap;
  83.    static int cxClient, cyClient;
  84.    HDC hdc, memDC;
  85.    PAINTSTRUCT ps;
  86.    SYSTEMTIME st;
  87.    RECT rect;
  88.    int centerX, centerY;
  89.  
  90.    // Moved the declaration outside the switch
  91.    int statwidths[] = { -1 };
  92.  
  93.    switch (message) {
  94.    case WM_CREATE:
  95.        // Enable double buffering
  96.        SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_CLIPCHILDREN);
  97.  
  98.        hwndStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
  99.            WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
  100.            hwnd, (HMENU)ID_STATUSBAR, GetModuleHandle(NULL), NULL);
  101.  
  102.        SendMessage(hwndStatus, SB_SETPARTS, 1, (LPARAM)statwidths);
  103.        SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)TEXT(""));
  104.        SetWindowSubclass(hwndStatus, StatusBarProc, 0, 0);
  105.        SetTimer(hwnd, ID_TIMER, 1000, NULL);
  106.        return 0;
  107.    case WM_KEYDOWN:
  108.        if (wParam == VK_ESCAPE) {
  109.            PostQuitMessage(0);
  110.            return 0;
  111.        }
  112.        // Add this new code block
  113.        else if (wParam == VK_F1) {
  114.            MessageBox(hwnd, TEXT("Analog Clock (incorporates SubClassing Controls) Programmed in C++ Win32 API (395 lines of code) by Entisoft Software (c) Evans Thorpemorton"), TEXT("Information"), MB_OK | MB_ICONINFORMATION);
  115.            return 0;
  116.        }
  117.        else if (wParam == VK_F2) {
  118.            HINSTANCE hInst = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
  119.            int calWidth = 350, calHeight = 300;
  120.            int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  121.            int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  122.            int xPos = (screenWidth - calWidth) / 2;
  123.            int yPos = (screenHeight - calHeight) / 2;
  124.            HWND hCal = CreateWindowEx(WS_EX_TOPMOST, MONTHCAL_CLASS, NULL,
  125.                WS_POPUP | WS_BORDER,
  126.                xPos, yPos, calWidth, calHeight, hwnd, NULL, hInst, NULL);
  127.            if (hCal) {
  128.                SendMessage(hCal, MCM_SETFIRSTDAYOFWEEK, (WPARAM)1, 0);
  129.                ShowWindow(hCal, SW_SHOW);
  130.            }
  131.            return 0;
  132.        }
  133.        break;
  134.    case WM_SIZE:
  135.        cxClient = LOWORD(lParam);
  136.        cyClient = HIWORD(lParam);
  137.        if (hBitmap) {
  138.            DeleteObject(hBitmap);
  139.        }
  140.        hdc = GetDC(hwnd);
  141.        hBitmap = CreateCompatibleBitmap(hdc, cxClient, cyClient);
  142.        ReleaseDC(hwnd, hdc);
  143.        SendMessage(hwndStatus, WM_SIZE, 0, 0);
  144.        return 0;
  145.    case WM_PAINT:
  146.        hdc = BeginPaint(hwnd, &ps);
  147.        memDC = CreateCompatibleDC(hdc);
  148.        SelectObject(memDC, hBitmap);
  149.        GetClientRect(hwnd, &rect);
  150.        centerX = (rect.right - rect.left) / 2;
  151.        centerY = ((rect.bottom - rect.top) - 20) / 2;
  152.        FillRect(memDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
  153.        DrawClock(memDC, centerX, centerY, min(centerX, centerY) - CLOCK_MARGIN);
  154.        BitBlt(hdc, 0, 0, cxClient, cyClient, memDC, 0, 0, SRCCOPY);
  155.        DeleteDC(memDC);
  156.        EndPaint(hwnd, &ps);
  157.        return 0;
  158.    case WM_TIMER:
  159.        GetLocalTime(&st);
  160.        UpdateStatusBar(hwndStatus, st);
  161.        InvalidateRect(hwnd, NULL, FALSE);
  162.        return 0;
  163.    case WM_DESTROY:
  164.        KillTimer(hwnd, ID_TIMER);
  165.        if (hBitmap) DeleteObject(hBitmap);
  166.        // Add this to WM_DESTROY handler:
  167.        Gdiplus::GdiplusShutdown(gdiplusToken);
  168.        PostQuitMessage(0);
  169.        return 0;
  170.    }
  171.    return DefWindowProc(hwnd, message, wParam, lParam);
  172. }
  173.  
  174. void DrawClock(HDC hdc, int x, int y, int r) {
  175.    SYSTEMTIME st;
  176.    GetLocalTime(&st);
  177.  
  178.    // Create memory DC for double buffering
  179.    HDC memDC = CreateCompatibleDC(hdc);
  180.    HBITMAP hBitmap = CreateCompatibleBitmap(hdc, 2 * r, 2 * r);
  181.    HBITMAP hOldBitmap = (HBITMAP)SelectObject(memDC, hBitmap);
  182.  
  183.    // Calculate center point for the memory DC
  184.    int centerX = r;  // Center X in memory DC coordinates
  185.    int centerY = r;  // Center Y in memory DC coordinates
  186.  
  187.    // Draw gradient background
  188.    TRIVERTEX vertex[2];
  189.    vertex[0].x = 0;
  190.    vertex[0].y = 0;
  191.    vertex[0].Red = 0xc000;
  192.    vertex[0].Green = 0xc000;
  193.    vertex[0].Blue = 0xc000;
  194.    vertex[0].Alpha = 0x0000;
  195.    vertex[1].x = 2 * r;
  196.    vertex[1].y = 2 * r;
  197.    vertex[1].Red = 0x4000;
  198.    vertex[1].Green = 0x4000;
  199.    vertex[1].Blue = 0x4000;
  200.    vertex[1].Alpha = 0x0000;
  201.    GRADIENT_RECT gRect = { 0, 1 };
  202.    GradientFill(memDC, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_H);
  203.  
  204.    // Draw clock outline
  205.    HPEN hPen = CreatePen(PS_SOLID, 8, RGB(128, 128, 128));
  206.    HPEN hOldPen = (HPEN)SelectObject(memDC, hPen);
  207.    Ellipse(memDC, 0, 0, 2 * r, 2 * r);
  208.  
  209.    // Draw hour numbers
  210.    HFONT hFont = CreateFont(12, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
  211.        ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  212.        DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, TEXT("Segoe UI"));
  213.    HFONT hOldFont = (HFONT)SelectObject(memDC, hFont);
  214.    SetTextColor(memDC, RGB(112, 128, 144));
  215.    SetTextAlign(memDC, TA_CENTER | TA_BASELINE);
  216.    SetBkMode(memDC, TRANSPARENT);
  217.  
  218.    for (int i = 1; i <= 12; i++) {
  219.        double angle = (i * 30 - 90) * (3.14159265358979323846 / 180);
  220.        int numX = centerX + (int)(cos(angle) * (r - 25));
  221.        int numY = centerY + (int)(sin(angle) * (r - 25));
  222.        WCHAR numStr[3];
  223.        wsprintf(numStr, L"%d", i);
  224.        TextOut(memDC, numX, numY, numStr, lstrlen(numStr));
  225.    }
  226.  
  227.    // Draw markers
  228.    for (int i = 0; i < 60; i++) {
  229.        if (i % 5 == 0) {
  230.            DrawHand(memDC, centerX, centerY, i * 6, r - 10, RGB(0, 0, 0), false);
  231.        }
  232.        else {
  233.            DrawHand(memDC, centerX, centerY, i * 6, r - 5, RGB(0, 0, 0), false);
  234.        }
  235.    }
  236.  
  237.    // Draw clock hands
  238.    DrawHand(memDC, centerX, centerY, st.wSecond * 6, r - 30, RGB(255, 0, 0));
  239.    DrawHand(memDC, centerX, centerY, (st.wMinute * 6) + (st.wSecond / 10.0), r - 60, RGB(0, 0, 0));
  240.    DrawHand(memDC, centerX, centerY, (st.wHour * 30) + (st.wMinute / 2.0), r - 90, RGB(0, 0, 255));
  241.  
  242.    // Draw center white circle
  243.    HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
  244.    HBRUSH oldBrush = (HBRUSH)SelectObject(memDC, whiteBrush);
  245.    Ellipse(memDC, centerX - 10, centerY - 10, centerX + 10, centerY + 10);
  246.    SelectObject(memDC, oldBrush);
  247.    DeleteObject(whiteBrush);
  248.  
  249.    // Add AM/PM text
  250.    SelectObject(memDC, hOldFont);
  251.    DeleteObject(hFont);
  252.  
  253.    hFont = CreateFont(18, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
  254.        ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  255.        DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, TEXT("Segoe UI"));
  256.    SelectObject(memDC, hFont);
  257.    SetTextColor(memDC, RGB(128, 128, 128));
  258.    TextOut(memDC, centerX, centerY + (r - 70), st.wHour < 12 ? TEXT("AM") : TEXT("PM"), 2);
  259.  
  260.    // Copy the final result to the screen
  261.    BitBlt(hdc, x - r, y - r, 2 * r, 2 * r, memDC, 0, 0, SRCCOPY);
  262.  
  263.    // Final cleanup
  264.    SelectObject(memDC, hOldFont);
  265.    SelectObject(memDC, hOldPen);
  266.    SelectObject(memDC, hOldBitmap);
  267.    DeleteObject(hFont);
  268.    DeleteObject(hPen);
  269.    DeleteObject(hBitmap);
  270.    DeleteDC(memDC);
  271. }
  272.  
  273. void DrawHand(HDC hdc, int x, int y, double angle, int length, COLORREF color, bool drawLineToCenter) {
  274.    double radian = (angle - 90) * (3.14159265358979323846 / 180);
  275.    int extensionLength = length * 0.1;
  276.  
  277.    // Create GDI+ Graphics object
  278.    Gdiplus::Graphics graphics(hdc);
  279.    graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
  280.  
  281.    if (drawLineToCenter) {
  282.        if (color == RGB(255, 0, 0)) {  // Second hand
  283.            Gdiplus::Point pts[3];
  284.            
  285.            // Calculate points
  286.            pts[0].X = x + (int)(cos(radian) * length);
  287.            pts[0].Y = y + (int)(sin(radian) * length);
  288.  
  289.            double perpRadian = radian + 3.14159265358979323846 / 2;
  290.            int baseWidth = 8;
  291.            pts[1].X = x - (int)(cos(radian) * extensionLength) + (int)(cos(perpRadian) * baseWidth);
  292.            pts[1].Y = y - (int)(sin(radian) * extensionLength) + (int)(sin(perpRadian) * baseWidth);
  293.            pts[2].X = x - (int)(cos(radian) * extensionLength) - (int)(cos(perpRadian) * baseWidth);
  294.            pts[2].Y = y - (int)(sin(radian) * extensionLength) - (int)(sin(perpRadian) * baseWidth);
  295.  
  296.            // Create brush and pen
  297.            Gdiplus::SolidBrush brush(Gdiplus::Color(255, GetRValue(color), GetGValue(color), GetBValue(color)));
  298.            graphics.FillPolygon(&brush, pts, 3);
  299.        }
  300.        else {
  301.            // Hour and minute hands
  302.            int penThickness = (color == RGB(0, 0, 255)) ? 6 : 4;
  303.            Gdiplus::Pen pen(Gdiplus::Color(255, GetRValue(color), GetGValue(color), GetBValue(color)),
  304.                            (Gdiplus::REAL)penThickness);
  305.            
  306.            int endX = x + (int)(cos(radian) * length);
  307.            int endY = y + (int)(sin(radian) * length);
  308.            int startX = x - (int)(cos(radian) * extensionLength);
  309.            int startY = y - (int)(sin(radian) * extensionLength);
  310.  
  311.            graphics.DrawLine(&pen, startX, startY, endX, endY);
  312.        }
  313.    }
  314.    else {
  315.        // Markers
  316.        int markerThickness = ((int)angle % 30 == 0) ? 4 : 2;
  317.        int markerLength = ((int)angle % 30 == 0) ? 15 : 10; // Longer markers for hours
  318.  
  319.        Gdiplus::Pen pen(Gdiplus::Color(255, GetRValue(color), GetGValue(color), GetBValue(color)),
  320.                        (Gdiplus::REAL)markerThickness);
  321.  
  322.        int endX = x + (int)(cos(radian) * length);
  323.        int endY = y + (int)(sin(radian) * length);
  324.        int startX = endX + (int)(cos(radian) * markerLength);
  325.        int startY = endY + (int)(sin(radian) * markerLength);
  326.  
  327.        graphics.DrawLine(&pen, endX, endY, startX, startY);
  328.    }
  329. }
  330.  
  331. void UpdateStatusBar(HWND hwndStatus, SYSTEMTIME st) {
  332.    TCHAR szStatus[256];
  333.    TCHAR szDayOfWeek[32], szMonth[32];
  334.    TCHAR szSuffix[3];
  335.    int weekNumber;
  336.  
  337.    GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("dddd"), szDayOfWeek, sizeof(szDayOfWeek) / sizeof(TCHAR));
  338.    GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("MMMM"), szMonth, sizeof(szMonth) / sizeof(TCHAR));
  339.  
  340.    SYSTEMTIME newYearDay = { st.wYear, 1, 0, 1 };
  341.    FILETIME ftNewYearDay, ftCurrentDay;
  342.    SystemTimeToFileTime(&newYearDay, &ftNewYearDay);
  343.    SystemTimeToFileTime(&st, &ftCurrentDay);
  344.    ULARGE_INTEGER uliNewYearDay, uliCurrentDay;
  345.    uliNewYearDay.LowPart = ftNewYearDay.dwLowDateTime;
  346.    uliNewYearDay.HighPart = ftNewYearDay.dwHighDateTime;
  347.    uliCurrentDay.LowPart = ftCurrentDay.dwLowDateTime;
  348.    uliCurrentDay.HighPart = ftCurrentDay.dwHighDateTime;
  349.    weekNumber = (int)(((uliCurrentDay.QuadPart - uliNewYearDay.QuadPart) / (7 * 24 * 60 * 60 * 10000000ULL)) + 1);
  350.  
  351.    if (st.wDay == 1 || st.wDay == 21 || st.wDay == 31)
  352.        _tcscpy_s(szSuffix, _T("st"));
  353.    else if (st.wDay == 2 || st.wDay == 22)
  354.        _tcscpy_s(szSuffix, _T("nd"));
  355.    else if (st.wDay == 3 || st.wDay == 23)
  356.        _tcscpy_s(szSuffix, _T("rd"));
  357.    else
  358.        _tcscpy_s(szSuffix, _T("th"));
  359.  
  360.    // Get time zone information
  361.    TIME_ZONE_INFORMATION tzi;
  362.    GetTimeZoneInformation(&tzi);
  363.    int bias = -(tzi.Bias); // Negative because Bias is in opposite direction
  364.    int hours = bias / 60;
  365.    int minutes = abs(bias % 60);
  366.  
  367.    StringCbPrintf(szStatus, sizeof(szStatus),
  368.        TEXT("%s %d%s %s %d #%d %d/%02d/%04d %02d:%02d:%02d %s GMT%s%d:%02d"),
  369.        szDayOfWeek, st.wDay, szSuffix, szMonth, st.wYear, weekNumber,
  370.        st.wDay, st.wMonth, st.wYear,
  371.        (st.wHour == 0 || st.wHour == 12) ? 12 : st.wHour % 12,
  372.        st.wMinute, st.wSecond,
  373.        st.wHour < 12 ? TEXT("AM") : TEXT("PM"),
  374.        (hours >= 0) ? TEXT("+") : TEXT(""),
  375.        hours,
  376.        minutes);
  377.  
  378.    SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)szStatus);
  379.    InvalidateRect(hwndStatus, NULL, TRUE);
  380. }
  381.  
  382. LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
  383.    if (uMsg == WM_PAINT) {
  384.        PAINTSTRUCT ps;
  385.        HDC hdc = BeginPaint(hWnd, &ps);
  386.  
  387.        RECT rc;
  388.        GetClientRect(hWnd, &rc);
  389.  
  390.        TCHAR szText[256];
  391.        int len = SendMessage(hWnd, SB_GETTEXT, 0, (LPARAM)szText);
  392.  
  393.        SetBkMode(hdc, TRANSPARENT);
  394.        SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  395.  
  396.        DrawText(hdc, szText, len, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  397.  
  398.        EndPaint(hWnd, &ps);
  399.        return 0;
  400.    }
  401.  
  402.    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
  403. }
  404. ```
Add Comment
Please, Sign In to add comment