Clock
Clock
Clock
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;
RegisterClass(&wc);
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");
// 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);
// Draw time
RECT rect;
GetClientRect(hwnd, &rect);
DrawText(hdc, timeStr, -1, &rect, DT_CENTER | DT_TOP | DT_SINGLELINE);
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);
}