Win32 - 无法使用RedrawWindow()触发WM_PAINT消息

时间:2014-04-07 11:44:41

标签: c++ winapi win32gui

我试图从WM_TIMER触发WM_PAINT消息;计时器工作,但RedrawWindow()函数似乎没有做任何事情。我做错了什么?

这是我的回调函数:

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;
}

1 个答案:

答案 0 :(得分:5)

由于x被定义为函数中的局部变量,因此在调用函数时它总是获得值true。也就是说,WM_PAINT中的代码永远不会到达else的{​​{1}}分支。

例如,尝试将if的定义更改为x,以便进行切换工作。

此外,您需要使窗口内容无效以重新绘制它:

static bool x = true;