Question

I am trying to trigger a WM_PAINT message form WM_TIMER; the timer works, but RedrawWindow() function does not seem to do anything. What am I doing wrong?

Here is my Callback function:

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;
}
Was it helpful?

Solution

As x is defined as a local variable in your function, it always gets the value true when the function is called. That is, the code in WM_PAINT never gets to the else branch of the if.

Try, for example, changing the definition of x to static bool x = true; to get the toggling work.

Additionally, you need to invalidate the window's contents to get it redrawn:

RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE | RDW_INTERNALPAINT);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top