我試圖觸發一個WM_PAINT消息窗體WM_TIMER;定時器工作,但RedrawWindow()函數似乎沒有做任何事情。我究竟做錯了什麼?Win32 - 無法用RedrawWindow觸發WM_PAINT消息()
這裏是我的回調函數:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
PAINTSTRUCT Ps;
COLORREF clrBlue = RGB(25, 55, 200);
RECT Recto = { 20, 28, 188, 128 };
COLORREF clrAqua = RGB(128, 255, 255);
COLORREF clrRed = RGB(255, 25, 5);
static bool x = true;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_TIMER:
//InvalidateRect(hWnd ,NULL , FALSE);
//RedrawWindow(hWnd , NULL , NULL , RDW_INVALIDATE);
RedrawWindow(hWnd,NULL,NULL,RDW_INTERNALPAINT);
break;
case WM_PAINT:
if(x)
{
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, clrRed);
TextOut(hdc, 50, 42, L"Some text", 13);
EndPaint(hWnd, &ps);
toggle(x);
}
else
{
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, clrRed);
TextOut(hdc, 50, 42, L"Another text", 13);
EndPaint(hWnd, &ps);
toggle(x);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
如果你想立即重繪隨後也嘗試添加了'RDW_UPDATENOW'和/或'RDW_ERASENOW'標誌旁邊的' RDW_INVALIDATE'。順便說一下,你的'WM_PAINT'處理程序是否因'RedrawWindow'調用而執行? – pasztorpisti
13在您的TextOut調用中字符數不正確,這可能會導致未定義的行爲。 – marcinj
InvalidateRect就足夠了。確保您的計時器實際上正在被解僱,並確保您的計時器時間間隔足夠慢,以便實際進行重新繪製。 – tenfour