alien_fx_fiend

Analog-Clock-win32 (FLAWED) ATTEMPT AT Overextended Hands + Thicker Markers

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