3.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's MSG 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

翻译:

SetTimer函数一个指定终止时间的定时器
UINT SetTimer(
  HWND hWnd ,              // 相关联窗口
  UINT nIDEvent ,          // 时间标识符
  UINT uElapse ,           // 设定的时间值
  TIMERPROC lpTimerFunc    // 计时器程序地址
);
参数:
hWnd:与计时器相关联的窗口的句柄。必须被调用的线程所有,如果参数为空,没有窗口与计时器相连,nIDEvent参数被忽略。
nIDEvent:指定一个非零的计时器标识,如果hWnd为空该参数被忽略。
uElapse:指定截止时间,以毫秒计。
lpTimerFunc:指向当时间到时,要通报的函数,要获得更多信息参见TimerProc函数。
如果lpTimerFunc为空,系统发出WM_TIMER消息给应用程序队列。消息的MSG结构体包含hWnd参数的值。
返回值:如果调用成功,是一个识别新的计时器的整数,如果存在应用程序可发送这个值或字符串标识给KillTimer函数来销毁计时器。如果创建计时器失败,返回值为零要得到更多的错误消息,请调用GetLastError函数。
备注:应用程序可以在包含WM_TIMER case声明的窗口处理函数中处理WM_TIMER消息,也可以指定TimerProc回调函数处理。当指定里TimerProc调回函数,默认窗口程序调用调回函数,在处理WM_TIMER消息时。因此需要在调用的线程里分派消息,即使使用TimerProc代替WM_TIMER的处理。
WM_TIMER消息的wParam参数包含着nIDEvent参数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
settimer函数是一种用于设置定时器的函数。它可以在指定的时间间隔后触发一个回调函数或执行一段代码。settimer函数通常用于需要在一段时间后执行某个任务的情况,比如延迟执行某个函数或者定时执行某个操作。 settimer函数的用法如下: 1. 首先,需要包含相应的头文件,比如在C++中可以包含<ctime>头文件。 2. 然后,使用settimer函数来设置定时器。settimer函数通常需要传入以下参数: - 回调函数或代码块:指定在定时器触发时要执行的回调函数或代码块。 - 时间间隔:指定定时器触发的时间间隔,可以是毫秒、秒或其他时间单位。 3. 最后,启动定时器,使其开始计时。 以下是一个示例代码,演示了settimer函数的用法: ```cpp #include <iostream> #include <ctime> void callback() { std::cout << "定时器触发了!" << std::endl; } int main() { // 设置定时器,每隔1秒触发一次回调函数 std::clock_t interval = 1000; // 1秒 std::clock_t start_time = std::clock(); while (true) { std::clock_t current_time = std::clock(); std::clock_t elapsed_time = (current_time - start_time) / (CLOCKS_PER_SEC / 1000); // 转换为毫秒 if (elapsed_time >= interval) { callback(); start_time = current_time; } } return 0; } ``` 上述示例代码中,我们使用了<ctime>头文件中的clock函数来获取当前时间,并通过计算时间间隔来判断是否触发定时器。在每次定时器触发时,会调用callback函数来输出一条信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值