0% found this document useful (0 votes)
20 views3 pages

Clock

Uploaded by

ashamisha234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Clock

Uploaded by

ashamisha234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <windows.

h>
#include <ctime>
#include <string>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

// Global variables
const wchar_t CLASS_NAME[] = L"Transparent Clock";
HWND hwnd;
HFONT hFont;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine,


int nCmdShow) {
// Register the window class
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);

RegisterClass(&wc);

// Create the window


hwnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TOPMOST, // Extended window style
CLASS_NAME, // Window class
L"Transparent Clock", // Window text
WS_POPUP, // Window style
CW_USEDEFAULT, CW_USEDEFAULT, // Position
300, 150, // Size
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional data
);

if (hwnd == NULL) {
return 0;
}

// Create font
hFont = CreateFont(48, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");

// Set the window transparent


SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 200, LWA_ALPHA);

// Show the window


ShowWindow(hwnd, nCmdShow);

// Set timer for updating clock (every second)


SetTimer(hwnd, 1, 1000, NULL);

// Message loop
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

DeleteObject(hFont);
return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

// Set transparent background


SetBkMode(hdc, TRANSPARENT);

// Select font and set text color


SelectObject(hdc, hFont);
SetTextColor(hdc, RGB(255, 255, 255));

// Get current time


time_t now = time(0);
tm ltm;
localtime_s(&ltm, &now);

// Format time string


wchar_t timeStr[32];
swprintf_s(timeStr, L"%02d:%02d:%02d",
ltm.tm_hour, ltm.tm_min, ltm.tm_sec);

// Format date string


wchar_t dateStr[64];
wchar_t* weekdays[] = {L"Sunday", L"Monday", L"Tuesday",
L"Wednesday", L"Thursday", L"Friday", L"Saturday"};
wchar_t* months[] = {L"January", L"February", L"March", L"April",
L"May", L"June", L"July", L"August", L"September",
L"October", L"November", L"December"};

swprintf_s(dateStr, L"%ls, %ls %d, %d",


weekdays[ltm.tm_wday],
months[ltm.tm_mon],
ltm.tm_mday,
ltm.tm_year + 1900);

// Draw time
RECT rect;
GetClientRect(hwnd, &rect);
DrawText(hdc, timeStr, -1, &rect, DT_CENTER | DT_TOP | DT_SINGLELINE);

// Draw date (smaller font)


HFONT dateFont = CreateFont(24, 0, 0, 0, FW_NORMAL, FALSE, FALSE,
FALSE,
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");

SelectObject(hdc, dateFont);
rect.top += 60; // Move down for date
DrawText(hdc, dateStr, -1, &rect, DT_CENTER | DT_TOP | DT_SINGLELINE);

DeleteObject(dateFont);
EndPaint(hwnd, &ps);
return 0;
}

case WM_TIMER:
InvalidateRect(hwnd, NULL, TRUE);
return 0;

case WM_LBUTTONDOWN:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

You might also like