CWinThread种中使用SetTimer WM_TIMER wParam的值不固定 求解释
CWinThread类中使用SetTimer WM_TIMER wParam的值不固定 求解释
SetTimer(NULL,1,10000,NULL);
SetTimer(NULL,2,15000,NULL);
处理消息时
if(msg.message==WM_TIMER)
{
switch(msg.wParam)//多个定时器
{
case 1: AfxMessageBox(_T("定时器1")); break;
case 2:AfxMessageBox(_T("定时器2"));break;
}
}
调试发现每次启程序 wParam的值都不一样 不是说wParam就是计时器id吗,不过计时器id相对值倒是对的 计时器2的wParam就比计时器1的大1.
难道是因为非窗口类中SetTimer第1个参数为空 id就是随机?还是窗口类也是这样第一次产生的wParam本身就随机?
------最佳解决方案--------------------
注意查看
MSDN
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
就是说如果hWnd为空(也就是你第一个参数为空的话),后面的1会去找存在hWnd相对应的匹配,如果没有就自动生成一个随机ID
------其他解决方案--------------------
本帖最后由 VisualEleven 于 2012-10-27 14:15:31 编辑 If the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer.
用SetTimer的返回值
SetTimer(NULL,1,10000,NULL);
SetTimer(NULL,2,15000,NULL);
处理消息时
if(msg.message==WM_TIMER)
{
switch(msg.wParam)//多个定时器
{
case 1: AfxMessageBox(_T("定时器1")); break;
case 2:AfxMessageBox(_T("定时器2"));break;
}
}
调试发现每次启程序 wParam的值都不一样 不是说wParam就是计时器id吗,不过计时器id相对值倒是对的 计时器2的wParam就比计时器1的大1.
难道是因为非窗口类中SetTimer第1个参数为空 id就是随机?还是窗口类也是这样第一次产生的wParam本身就随机?
------最佳解决方案--------------------
注意查看
MSDN
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
就是说如果hWnd为空(也就是你第一个参数为空的话),后面的1会去找存在hWnd相对应的匹配,如果没有就自动生成一个随机ID
------其他解决方案--------------------
本帖最后由 VisualEleven 于 2012-10-27 14:15:31 编辑 If the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer.
用SetTimer的返回值