#include
#include
#include
#include
time_t end;
void CenterWindow(HWND hWnd_self)
{
HWND hWnd_parent;
RECT rw_self, rc_parent, rw_parent;
int xpos, ypos;
hWnd_parent = GetParent(hWnd_self);
if (NULL == hWnd_parent)
hWnd_parent = GetDesktopWindow();
GetWindowRect(hWnd_parent, &rw_parent);
GetClientRect(hWnd_parent, &rc_parent);
GetWindowRect(hWnd_self, &rw_self);
xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2;
ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2;
SetWindowPos(
hWnd_self, NULL,
xpos, ypos, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE
);
struct tm t;
t.tm_year = 2025 - 1900;
t.tm_mon = 11 - 1;
t.tm_mday = 1;
t.tm_hour = 8;
t.tm_min = 0;
t.tm_sec = 0;
end = mktime(&t);
}
// 窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR buf[128];
time_t start;
int nday, nhour, nmin, nsec, diff;
switch (uMsg)
{
case WM_CREATE:
SetTimer(hWnd, 10, 500, NULL);
CenterWindow(hWnd);
break;
case WM_TIMER:
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
time(&start);
diff = (int)difftime(end, start);
nday = diff / (24 * 3600);
nhour = (diff - nday * 24 * 3600) / 3600;
nmin = (diff - nday * 24 * 3600) % 3600 / 60;
nsec = (diff - nday * 24 * 3600) % 3600 % 60;
_stprintf_s(buf, 128, TEXT("%d天%02d时%02d分%02d秒"), nday, nhour, nmin, nsec);
DrawText(hdc, buf, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hWnd, &ps);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
INT WINAPI _tWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ INT nShowCmd)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
// 注册窗口类
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("BasicWindow");
RegisterClass(&wc);
// 创建窗口
hWnd = CreateWindow(TEXT("BasicWindow"), TEXT("注电倒计时"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, 0, 0, 240, 120, NULL, NULL, hInstance, NULL);
// 显示窗口
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
// 消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}