SetTimer 与 CWnd::SetTimer [英] SetTimer vs CWnd::SetTimer

查看:36
本文介绍了SetTimer 与 CWnd::SetTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MFC 的 CWnd::SetTimer 简单地调用 WinAPI 的 SetTimer.

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); }

但是 SetTimerCWnd::SetTimer 文档不一样.

But SetTimer and CWnd::SetTimer documents are not same.

SetTimer:
如果 hWnd 参数不是 NULL 并且 hWnd 指定的窗口已经有一个值为 nIDEvent 的定时器,然后现有的计时器被新的计时器取代.当 SetTimer 替换计时器时,计时器会重置.

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:
指定一个非零计时器标识符.如果计时器标识符是唯一的,则 SetTimer 将返回相同的值.否则,SetTimer 确定一个新的唯一值并返回该值.对于窗口计时器(具有 NULL 回调函数),该值必须仅对于与当前窗口关联的其他窗口计时器是唯一的.对于回调计时器,该值对于所有进程中的所有计时器必须是唯一的.因此,当您创建回调计时器时,返回值很有可能与您指定的值不同.

CWnd::SetTimer:
Specifies a nonzero timer identifier. If the timer identifier is unique, this same value is returned by SetTimer. Otherwise, SetTimer determines 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 不依赖于回调参数,总是替换现有的计时器.但是CWnd::SetTimer依赖于callback参数,如果指定了callback,可能会生成一个新的定时器ID,因为对于所有进程中的所有定时器来说,该值必须是唯一的.

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.

SetTimer:
如果函数成功并且hWnd 参数不是NULL,则返回值是一个非零整数.应用程序可以将 nIDEvent 参数的值传递给 KillTimer 函数以销毁计时器.

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:
如果函数成功,则为新定时器的定时器标识符.该值可能等于也可能不等于通过 nIDEvent 参数传入的值.应用程序应始终将返回值传递给 KillTimer 成员函数以终止计时器.

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 the nIDEvent parameter. An application should always pass the return value to the KillTimer member function to kill the timer.

SetTimer 不会生成新的计时器 id 并且不返回计时器 id,因此使用 nIDEvent 参数调用 KillTimer.但是 CWnd::SetTimer 在某些情况下会生成新的计时器 ID,因此使用返回值调用 KillTimer.

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?

我想使用带有回调的 WinAPI SetTimer 并且它可以在我的电脑上运行.但是如果某些平台不替换现有的计时器,我无法接受回调计时器.

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.

推荐答案

据我所知,MFC 文档不正确.我已经进行了广泛的测试,只要窗口相同,计时器总是会替换以前的计时器.无论有没有回调都是如此.

MFC documentation is incorrect, as far as I can tell. I have tested extensively, and the timers always replace the previous timer, provided the window is the same. This is true with callbacks and without.

通过回调,我运行了以下测试:

With a callback, I ran the following test:

static void CALLBACK MyTimerProc(HWND hWnd, UINT nMsg, UINT_PTR nIDEvent, DWORD dwTime) {
    KillTimer(hWnd, nIDEvent);
}
...
timerID = SetTimer(2, 1000, MyTimerProc);
timerID = SetTimer(2, 1100, MyTimerProc);
timerID = SetTimer(4, 1200, MyTimerProc);
timerID = GetParentFrame()->SetTimer(4, 1300, MyTimerProc);

结果是(来自 VS 调试器跟踪):

Results were (from VS debugger trace):

timerID=2
timerID=2
timerID=4
timerID=4
nIDEvent=2, hWnd=0x00000000002d0bb8
nIDEvent=4, hWnd=0x00000000002d0bb8
nIDEvent=4, hWnd=0x0000000000140bd0

最后一次 SetTimer 调用使用了不同的窗口,它两次提供了相同的事件.但每次,返回值都与传递给计时器的值相同.并且 nIDEvent 使用相同的值.

The last SetTimer call used a different window, which gave the same event twice. But every time, the return value is identical to the value passed for the timer. And the same value is used for nIDEvent.

CWnd 文档要么是过时的,要么是出于极其谨慎的考虑——我们知道返回值是 ID,所以我们应该始终使用它.

The CWnd documentation is either out of date, or acting out of extreme caution--we know the return value is the ID, so we should always use that.

然而,一种说法显然是错误的:

However, one statement is clearly false:

对于回调定时器,所有进程中的所有定时器的值必须是唯一的

我刚刚演示了我可以在相同进程中使用相同的 ID 两次,并且仍然接收两个事件.

I just demonstrated I could use the same ID twice, within the same process, and still receive both events.

这篇关于SetTimer 与 CWnd::SetTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆