#2
rjsp2023-04-04 09:47
程序代码: #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; } |
程序代码:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
.........
SYSTEMTIME st;
GetLocalTime(&st);
TCHAR cha[9] = {};
swprintf_s(cha, _T("%hu%hs%hu%hs%hu"), st.wHour, ":", st.wMinute, ":", st.wSecond);
//InvalidateRect(hWnd, NULL, true);
DrawText(hdc, cha, -1, &rect, DT_WORDBREAK);
//Sleep(1000);
.........
}
}
}
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
.........
SYSTEMTIME st;
GetLocalTime(&st);
TCHAR cha[9] = {};
swprintf_s(cha, _T("%hu%hs%hu%hs%hu"), st.wHour, ":", st.wMinute, ":", st.wSecond);
//InvalidateRect(hWnd, NULL, true);
DrawText(hdc, cha, -1, &rect, DT_WORDBREAK);
//Sleep(1000);
.........
}
}
}
为什么DrawText放在 switch (message)循环里不会重复打印时间st.wSecond变化啊?
加上InvalidateRect后,清除太快,直接都看不到显示了。
Sleep(1000)虽然能显示变化,但整个循环每1秒都会卡定。
要用什么方法刷新动态字符呢?
[此贴子已经被作者于2023-4-4 08:48编辑过]