Background
MFC's CWnd::SetTimer calls WinAPI's SetTimer simply.
_AFXWIN_INLINE UINT_PTR CWnd::SetTimer(UINT_PTR nIDEvent, UINT nElapse,
void (CALLBACK* lpfnTimer)(HWND, UINT, UINT_PTR, DWORD))
{ ASSERT(::IsWindow(m_hWnd)); return ::SetTimer(m_hWnd, nIDEvent, nElapse,
lpfnTimer); }
But SetTimer and CWnd::SetTimer documents are not same.
1. nIDEvent parameter
SetTimer:
If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset.
CWnd::SetTimer:
Specifies a nonzero timer identifier. If the timer identifier is unique, this same value is returned bySetTimer. Otherwise,SetTimerdetermines a new unique value and returns that. For a window timer (which has a NULL callback function), the value must be unique only for other windows timers that are associated with the current window. For a callback timer, the value must be unique for all timers in all processes. Therefore, when you create a callback timer, it is more likely that the returned value might differ from the value you specify.
SetTimer does not depend on callback parameter and always replaces the existing timer. But CWnd::SetTimer depends on callback parameter, and if callback specified, a new timer ID may be generated because the value must be unique for all timers in all processes.
2. Return value
SetTimer:
If the function succeeds and the hWnd parameter is not NULL, then the return value is a nonzero integer. An application can pass the value of the nIDEvent parameter to the KillTimer function to destroy the timer.
CWnd::SetTimer:
The timer identifier of the new timer if the function is successful. This value may or may not be equal to the value passed in through thenIDEventparameter. An application should always pass the return value to the KillTimer member function to kill the timer.
SetTimer does not generate new timer id and does not return timer id, so call KillTimer with nIDEvent parameter. But CWnd::SetTimer generates new timer id in some case, so call KillTimer with the returned value.
Thus, which document is correct?
I want to use WinAPI SetTimer with a callback and it works on my pc. But if some platform does not replace the existing timer, I could not accept the callback timer.
SetTimer()and it seems to work as advertised. If you really want to know, you should run some simple tests. - Paul Sanders