今天在测试一个小东东,忽然不知道settimer肿么用了,查了查msdn,仔细的看了看使用方法,发现参数很有意思:
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. WhenSetTimer 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.
这是nIDEvent的解释,亮点是: 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
就是说,如果这个nidevent如果已经存在,那么新传入的这个就会替代之前的那个,并且重新开始计时。
这时自己犯2的做了个小测验:
首先设置两个timer,1(10s),2(20s),使用mfc中默认的消息响应函数。
开始运行,一切正常。
然后,用settimer设置了另外一个nidevent为1(10S)的timer,并且其中的回调函数更改。
windows默认函数:
void Ctest_linux_esam_listDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
// esam_delete_expired_ele_clean_list();
switch(nIDEvent)
{
case 1:
AfxMessageBox(_T("OnTimer event 1"));
break;
case 2:
AfxMessageBox(_T("OnTimer event 2"));
break;
default:
break;
}
CDialog::OnTimer(nIDEvent);
}
自己的timeproc:
VOID CALLBACK timerfunc(
HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime
)
{
switch(idEvent)
{
case 1:AfxMessageBox(_T("enter timerfunc event 1"));
break;
case 2:AfxMessageBox(_T("enter timerfunc event 2"));
break;
default:break;
}
}
这时 亮点出来了:当nidevent为1的timeout了,会弹出enter timerfunc event 1。而在第二个timeout时,会弹出OnTimer event 2·····
感觉很神奇哇,回调都改变了,居然还能准确区分。猜想是调用settimer的时候windows为每个nidevent做了回调函数的地址映射,具体的没研究,如果哪位大神知道,请告知小弟~~