程序代码:
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"
#include <cwchar>
RECT g_SysTimeRect = { 0, 0, 100, 50 };
UINT g_SysTimeElapse = 100;
SYSTEMTIME g_SysTime;
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_COMMAND:
{
switch( LOWORD(wParam) )
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CREATE:
SetTimer( hWnd, 1, g_SysTimeElapse, nullptr );
break;
case WM_TIMER:
{
WORD wHour = g_SysTime.wHour;
WORD wMinute = g_SysTime.wMinute;
WORD wSecond = g_SysTime.wSecond;
GetLocalTime( &g_SysTime );
if( wHour!=g_SysTime.wHour || wMinute!=g_SysTime.wMinute || wSecond!=g_SysTime.wSecond )
InvalidateRect( hWnd, &g_SysTimeRect, FALSE );
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
{
wchar_t temp[9] = {};
swprintf( temp, 9, L"%02hu:%02hu:%02hu", g_SysTime.wHour, g_SysTime.wMinute, g_SysTime.wSecond );
DrawText( hdc, temp, -1, &g_SysTimeRect, DT_WORDBREAK );
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
KillTimer( hWnd, 1 );
PostQuitMessage( 0 );
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int __stdcall wWinMain( HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow )
{
WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_VCWIN32));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_VCWIN32);
wcex.lpszClassName = L"YourWindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW( &wcex );
HWND hWnd = CreateWindowW( L"YourWindowClass", L"YourTitle", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr );
if( !hWnd )
return FALSE;
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_VCWIN32));
MSG msg;
while( GetMessage(&msg,nullptr,0,0) )
{
if( !TranslateAccelerator(msg.hwnd,hAccelTable,&msg) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}