alien_fx_fiend

Analog Clock Win32 *FINAL RELEASE !*

Oct 22nd, 2024 (edited)
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.20 KB | Source Code | 0 0
  1. ==++"main.cpp" File 1/1 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, int, int, double, int, COLORREF, bool drawLineToCenter = true);
  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.     HPEN hPen = CreatePen(PS_SOLID, 8, RGB(128, 128, 128));
  166.     HPEN hOldPen = (HPEN)SelectObject(memDC, hPen);
  167.     Ellipse(memDC, 0, 0, 2 * r, 2 * r);
  168.     BitBlt(hdc, x - r, y - r, 2 * r, 2 * r, memDC, 0, 0, SRCCOPY);
  169.     SelectObject(memDC, hOldPen);
  170.     DeleteObject(hPen);
  171.     DeleteObject(hBitmap);
  172.     DeleteDC(memDC);
  173.     DrawHand(hdc, x, y, st.wSecond * 6, r - 30, RGB(255, 0, 0));
  174.     DrawHand(hdc, x, y, (st.wMinute * 6) + (st.wSecond / 10.0), r - 60, RGB(0, 0, 0));
  175.     DrawHand(hdc, x, y, (st.wHour * 30) + (st.wMinute / 2.0), r - 90, RGB(0, 0, 255));
  176.     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"));
  177.     HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
  178.     SetTextColor(hdc, RGB(112, 128, 144));
  179.     SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
  180.     SetBkMode(hdc, TRANSPARENT);
  181.     for (int i = 1; i <= 12; i++) {
  182.         double angle = (i * 30 - 90) * (3.14159265358979323846 / 180);
  183.         int numX = x + (int)(cos(angle) * (r - 25));
  184.         int numY = y + (int)(sin(angle) * (r - 25));
  185.         WCHAR numStr[3];
  186.         wsprintf(numStr, L"%d", i);
  187.         TextOut(hdc, numX, numY, numStr, lstrlen(numStr));
  188.     }
  189.  
  190.     // Add AM/PM text
  191.     DeleteObject(hFont);
  192.     hFont = CreateFont(18, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET,
  193.         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  194.         DEFAULT_PITCH | FF_SWISS, TEXT("Segoe UI"));
  195.     SelectObject(hdc, hFont);
  196.     SetTextColor(hdc, RGB(128, 128, 128));
  197.     TextOut(hdc, x, y + (r - 70), st.wHour < 12 ? TEXT("AM") : TEXT("PM"), 2);
  198.  
  199.     SelectObject(hdc, hOldFont);
  200.     DeleteObject(hFont);
  201.     for (int i = 0; i < 60; i++) {
  202.         if (i % 5 == 0) {
  203.             DrawHand(hdc, x, y, i * 6, r - 10, RGB(0, 0, 0), false);
  204.         }
  205.         else {
  206.             DrawHand(hdc, x, y, i * 6, r - 5, RGB(0, 0, 0), false);
  207.         }
  208.     }
  209. }
  210.  
  211. void DrawHand(HDC hdc, int x, int y, double angle, int length, COLORREF color, bool drawLineToCenter) {
  212.     double radian = (angle - 90) * (3.14159265358979323846 / 180);
  213.     int endX = x + (int)(cos(radian) * length);
  214.     int endY = y + (int)(sin(radian) * length);
  215.     HPEN hPen = CreatePen(PS_SOLID, 2, color);
  216.     HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  217.     if (drawLineToCenter) {
  218.         MoveToEx(hdc, x, y, NULL);
  219.         LineTo(hdc, endX, endY);
  220.     }
  221.     else {
  222.         MoveToEx(hdc, endX, endY, NULL);
  223.         LineTo(hdc, endX + (int)(cos(radian) * 5), endY + (int)(sin(radian) * 5));
  224.     }
  225.     SelectObject(hdc, hOldPen);
  226.     DeleteObject(hPen);
  227. }
  228.  
  229. void UpdateStatusBar(HWND hwndStatus, SYSTEMTIME st) {
  230.     TCHAR szStatus[256];
  231.     TCHAR szDayOfWeek[32], szMonth[32];
  232.     TCHAR szSuffix[3];
  233.     int weekNumber;
  234.  
  235.     GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("dddd"), szDayOfWeek, sizeof(szDayOfWeek) / sizeof(TCHAR));
  236.     GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, TEXT("MMMM"), szMonth, sizeof(szMonth) / sizeof(TCHAR));
  237.  
  238.     SYSTEMTIME newYearDay = { st.wYear, 1, 0, 1 };
  239.     FILETIME ftNewYearDay, ftCurrentDay;
  240.     SystemTimeToFileTime(&newYearDay, &ftNewYearDay);
  241.     SystemTimeToFileTime(&st, &ftCurrentDay);
  242.     ULARGE_INTEGER uliNewYearDay, uliCurrentDay;
  243.     uliNewYearDay.LowPart = ftNewYearDay.dwLowDateTime;
  244.     uliNewYearDay.HighPart = ftNewYearDay.dwHighDateTime;
  245.     uliCurrentDay.LowPart = ftCurrentDay.dwLowDateTime;
  246.     uliCurrentDay.HighPart = ftCurrentDay.dwHighDateTime;
  247.     weekNumber = (int)(((uliCurrentDay.QuadPart - uliNewYearDay.QuadPart) / (7 * 24 * 60 * 60 * 10000000ULL)) + 1);
  248.  
  249.     if (st.wDay == 1 || st.wDay == 21 || st.wDay == 31)
  250.         _tcscpy_s(szSuffix, _T("st"));
  251.     else if (st.wDay == 2 || st.wDay == 22)
  252.         _tcscpy_s(szSuffix, _T("nd"));
  253.     else if (st.wDay == 3 || st.wDay == 23)
  254.         _tcscpy_s(szSuffix, _T("rd"));
  255.     else
  256.         _tcscpy_s(szSuffix, _T("th"));
  257.  
  258.     // Get time zone information
  259.     TIME_ZONE_INFORMATION tzi;
  260.     GetTimeZoneInformation(&tzi);
  261.     int bias = -(tzi.Bias); // Negative because Bias is in opposite direction
  262.     int hours = bias / 60;
  263.     int minutes = abs(bias % 60);
  264.  
  265.     StringCbPrintf(szStatus, sizeof(szStatus),
  266.         TEXT("%s %d%s %s %d #%d %d/%02d/%04d %02d:%02d:%02d %s GMT%s%d:%02d"),
  267.         szDayOfWeek, st.wDay, szSuffix, szMonth, st.wYear, weekNumber,
  268.         st.wDay, st.wMonth, st.wYear,
  269.         (st.wHour == 0 || st.wHour == 12) ? 12 : st.wHour % 12,
  270.         st.wMinute, st.wSecond,
  271.         st.wHour < 12 ? TEXT("AM") : TEXT("PM"),
  272.         (hours >= 0) ? TEXT("+") : TEXT(""),
  273.         hours,
  274.         minutes);
  275.  
  276.     SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)szStatus);
  277.     InvalidateRect(hwndStatus, NULL, TRUE);
  278. }
  279.  
  280. LRESULT CALLBACK StatusBarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
  281.     if (uMsg == WM_PAINT) {
  282.         PAINTSTRUCT ps;
  283.         HDC hdc = BeginPaint(hWnd, &ps);
  284.  
  285.         RECT rc;
  286.         GetClientRect(hWnd, &rc);
  287.  
  288.         TCHAR szText[256];
  289.         int len = SendMessage(hWnd, SB_GETTEXT, 0, (LPARAM)szText);
  290.  
  291.         SetBkMode(hdc, TRANSPARENT);
  292.         SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  293.  
  294.         DrawText(hdc, szText, len, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  295.  
  296.         EndPaint(hWnd, &ps);
  297.         return 0;
  298.     }
  299.  
  300.     return DefSubclassProc(hWnd, uMsg, wParam, lParam);
  301. }
Add Comment
Please, Sign In to add comment