WM_TIMER puzzle

I am setting up a time using SetTimer(,,,,). I then try to catch the timer message with WM_TIMER. I find that I can only catch the end of timer message when the window is minimized. If the window is maximized I get the WM_PAINT messages, dozens of them and they seem to blank out the WM_TIMER messages so that they are not caught.

I get the same phenomenon using a procedure - only works with the window minimized.

I have a main window plus a dialog box. The timer is started up with a mouse click on the main window. The fact that it works when minimized tells me that the coding is sort of correct.

A puzzle.

Any comments please?

Paul
So looks like you have just added the ON_WM_TIMER() to your MESSAGE_MAP and added the OnTimer(UINT nIDEvent) to your window class. But you need to call SetTimer(...) and KillTimer(...) for the nIDEvent you are interested in

SetTimer( <your event id>, <milisecons>, NULL )

KillTimer( <your event id> )

HTH
I would have to see you code.

When you say you're getting dozens of WM_PAINT messages, is this when you're debugging your app?

And are you doing something to invalidate your window which causes the redraw?

Andy

PS Are you using MFC? If not, ajh32's comments about MESSAGE_MAP, etc are a bit spurious...

PPS This toy program displays a incrementing count which starts (from 0) when you double-click and stops when you double-click again. It's coded with the Windows API directly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <tchar.h>
#include <sstream>

const TCHAR szClassName [] = _T("Timer_Demo_Window_Class");
const TCHAR szWindowName[] = _T("Timer Demo");
const TCHAR szWindowName_Running[] = _T("Timer Demo (running)");

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int CALLBACK
_tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*lpCmdLine*/, int nCmdShow)
{
    WNDCLASSEX wcex = {0};
    wcex.cbSize        = sizeof(WNDCLASSEX);
    wcex.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    wcex.lpfnWndProc   = WndProc;
    wcex.hInstance     = hInstance;
    wcex.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszClassName = szClassName;
    wcex.hIconSm       = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    ATOM atom = RegisterClassEx(&wcex);
    if(0 == atom)
        return 0;

    HWND hWnd = CreateWindowEx( 0,
                                szClassName,
                                szWindowName,
                                WS_OVERLAPPEDWINDOW,
                                CW_USEDEFAULT, CW_USEDEFAULT,
                                CW_USEDEFAULT, CW_USEDEFAULT,
                                NULL,
                                NULL,
                                hInstance,
                                NULL );
    if(!hWnd)
        return 0;;

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static const TCHAR szFont[]      = _T("Arial");
    static const int   nFontSize     = 48;
    static const UINT  uTimerPeriond = 1000; // i.e. 1 sec (1000 msec)

    static HFONT s_hFont = NULL;
    static int   s_nCount = 0;
    static UINT  s_uTimerID = 0;

    switch (uMsg)
    {
        case WM_CREATE:
        {
            HDC hDC = GetDC(hWnd);

            LOGFONT logFont = {0};
            logFont.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
            logFont.lfWeight = FW_BOLD;
            _tcsncpy(logFont.lfFaceName, szFont, LF_FACESIZE);

            s_hFont = CreateFontIndirect(&logFont);

            ReleaseDC(hWnd, hDC);
        }
        break;

        case WM_DESTROY:
        {
            if(0 != s_uTimerID)
            {
                KillTimer(hWnd, s_uTimerID);
                s_uTimerID = 0;
            }

            if(s_hFont != NULL)
            {
                DeleteObject(s_hFont);
                s_hFont = NULL;
            }

            PostQuitMessage(0);
        }
        break;

        case WM_PAINT:
        {
            PAINTSTRUCT ps = {0};
            HDC hDC = BeginPaint(hWnd, &ps);

            RECT rcClient = {0};
            GetClientRect(hWnd, &rcClient);

            HFONT hFontOld = (HFONT)SelectObject(hDC, s_hFont);

#ifdef _UNICODE
            std::wostringstream oss;
#else
            std::ostringstream oss;
#endif
            oss << s_nCount;
            DrawText(hDC, oss.str().c_str(), oss.str().length(), &rcClient,
                DT_CENTER | DT_VCENTER | DT_SINGLELINE);

            SelectObject(hDC, hFontOld);

            EndPaint(hWnd, &ps);
        }
        break;

        case WM_LBUTTONDBLCLK:
        {
            if(0 == s_uTimerID)
            {
                SetWindowText(hWnd, szWindowName_Running);

                s_nCount = 0;
                InvalidateRect(hWnd, NULL, TRUE);

                s_uTimerID = SetTimer(hWnd, 1, uTimerPeriond, NULL);
            }
            else
            {
                SetWindowText(hWnd, szWindowName);

                KillTimer(hWnd, s_uTimerID);
                s_uTimerID = 0;
            }
        }
        break;

        case WM_TIMER:
        {
            ++s_nCount;
            InvalidateRect(hWnd, NULL, TRUE);
        }
        break;

        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    return 0;
}
Last edited on
Thanks for answering my query you both.

Andy, posting a script that is similar to mine and that works was so useful. It means I could slowly track down the discrepancy. My painting to the screen is long as it involves a music score with lines and notes and text etc so I had put it in a function. On debugging there were dozens of messages appearing in WM_PAINT, and these were hiding the WM_TIMER messages.

I finally tracked down that the positioning of InvalidateRect(hWnd, NULL, TRUE); was causing the problem.

I moved it to the top of the function for the time being and that solved my problem. I now get just the WM_PAINT messages that I should get and I am picking up the WM_TIMER messages OK. I don't understand the logic here and I reckon InvalidateRect(hWnd, NULL, TRUE); is in the wrong place but will investigate that later. Very happy bunny. Paul
Topic archived. No new replies allowed.