windows 多个定时器解决思路

windows 多个定时器
为什么在TimerProc()函数中传过来的idEvent不是我所定义的定时器的ID?输出一直是not found,求解!
C/C++ code

#include <windows.h> 
#include <iostream> 
#define TIMER1 1
#define TIMER2 2
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
    switch(idEvent)
    {
    case TIMER1:
        std::cout<<"this is timer1!"<<std::endl;
        break;
    case TIMER2:
        std::cout<<"this is timer2!"<<std::endl;
        break;
    default:
        std::cout<<"not found!"<<std::endl;
    }
}
void main() 
{
    MSG msg;                     
    SetTimer(NULL,TIMER1,5000,TimerProc);
    SetTimer(NULL,TIMER2,4000,TimerProc);
    int itemp; 
    while((itemp = GetMessage(&msg, NULL,NULL,NULL))&& (itemp!=0) &&  (-1 !=  itemp)) 
    {   
        if(msg.message == WM_TIMER)   
        {
            TranslateMessage(&msg);   
            DispatchMessage(&msg);
        }   
    }   
} 



------解决方案--------------------
查MSDN是Windows程序员必须掌握的技能之一。

SetTimer
The SetTimer function creates a timer with the specified time-out value. 

UINT SetTimer(
HWND hWnd, // handle of window for timer messages
UINT nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // address of timer procedure
);
 
Parameters
hWnd 
Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored. 
nIDEvent 
Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. 
uElapse 
Specifies the time-out value, in milliseconds. 
lpTimerFunc 
Pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. 
If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message'sMSG structure contains the value of the hWnd parameter. 

Return Values
If the function succeeds, the return value is an integer identifying the new timer. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. 

If the function fails to create a timer, the return value is zero. To get extended error information, call GetLastError.

Remarks
An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. 

The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter. 

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.

See Also
Timers Overview, Timer Functions, KillTimer,MSG, TimerProc, WM_TIMER 

 

------解决方案--------------------
楼主,你得这样。

&TimerProc

得改成:

SetTimer(NULL,TIMER1,5000,&TimerProc);
SetTimer(NULL,TIMER2,4000,&TimerProc);