alien_fx_fiend

Analog Clock (AlwaysOnTop) Tweak *FINAL RELEASE v2*

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