Search code examples
c++timerwm-paint

C++ WM_PAINT timer


How do I make a timer work in c++ WM_PAINT? I'm trying to "print" it via Wm_Paint, because at the moment I don't know another method of adding a timer, googling didn't help.

Here's what I have declared in CALLBACK:

TCHAR s[10], str[20] = _T("Seconds: ");
    static int t;

case WM_CREATE:


        SetTimer(hwnd, 1, 1000, NULL);

And here's how I try to draw the timer:

hdc = BeginPaint(hwnd, &ps);
        hBrush = CreateSolidBrush(g_color);
        hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0));
        holdPen = HPEN(SelectObject(hdc, hPen));
        holdBrush = (HBRUSH)SelectObject(hdc, hBrush);

_tcscat(str + 9, _itot(t, s, 10));
        TextOut(hdc, 10, 300, str, _tcsclen(str));

SelectObject(hdc, holdBrush);
        SelectObject(hdc, holdPen);
        DeleteObject(hPen);
        DeleteObject(hBrush);
        EndPaint(hwnd, &ps);

So far, it just prints out "Seconds: 0" and stops updating.


Solution

  • Ok so, for me to make it work, I've had to create a WM_TIMER case in my CALLBACK function, in the end it looked like this:

    //code above
    
    case WM_TIMER:
    t++;
    InvalidateRect(hwnd, NULL, TRUE);
    break;
    
    //code below