如题目所描述,三秒内用户不点击的话,则窗口隐藏,如果在3秒内,用户点击了一次,则再计时三秒.第一次是在win32下开发的,是在对话框里面的,当时我的处理方法是:
先设置定时器:
SetTimer(GetWindowHandle(),TIMER_BTN_ID,uBtnElapse,NULL);然后在对应的点击代码中添加
LRESULT Dlgxxx::OnLButtonDown(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
SetTimer(GetWindowHandle(),TIMER_BTN_ID,uBtnElapse,NULL);
return 0;
}这样子使用,经过测试发现,是可行的,每次点击之后,定时器就被清零了,重新计时.
这一次是在COM下实现的,没有对话框的概念了,所以只能使用:
SetTimer(0, 0, uClickedElapse, s_ClickedTimerProc);
故技重施,在对应的响应事件里面加:
SetTimer(0, 0, uClickedElapse, s_ClickedTimerProc);但是经过测试发现,事件每响应一次,增加一个定时器,响应10下,就出现了10个定时器.现在我的初步思路是:
在对应的响应事件里,先KillTimer,再SetTimer.
我觉的理论上时可行的.但是我想向各位大牛请教一下,有没有更简洁的实现方法?

解决方案 »

  1.   

    nIDEvent 
    [in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. 
    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. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. 如果没有指定窗口的话,还是就用KillTimer吧
      

  2.   

    KillTimer可行 
    假如对时间要求不严格,且有轮询线程,可以考虑用GetTickCount判断时间间隔
      

  3.   


    我电脑上的MSDN中关于SetTimer的解释有四个条目.一个版本是:
    hWnd 
    [in] 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 
    [in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. 另一个版本是:
    hWnd
    [in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If a NULL value for hWnd is passed in along with an nIDEvent of an existing timer, that timer will be replaced in the same way that an existing non-NULL hWnd timer will be.
    nIDEvent
    [in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. 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. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL. 
    我一直看的是第一个版本...所以kier2说的那些我都不懂...受教了!!!
      

  4.   

    即使在dlg中也要在OnTomer()中KillTimer()的。
      

  5.   


    不需要.
    MSDN上已经说了:
    [in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. 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. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL.  并且经过我测试发现不需要.