c++ - 如何在 C++ 中创建定时器 无法计算出 CALLBACK 的定时器 ID 值
问题描述
现在使用TimerProc。
VOID CALLBACK TimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
);
系统调用它来处理关联的 Timer 的 WM_TIMER 消息。让我们看一些代码。
#define IDT_TIMER1 1001
...
/* The Timer Procedure */
VOID CALLBACK TimerProc(HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime)
{
MessageBox(NULL, "One second is passed, the timer procedure is called, killing the timer", "Timer Procedure", MB_OK);
KillTimer(hwnd, idEvent);
}
...
/* Creating the timer */
SetTimer(hwnd, IDT_TIMER1, 1000, (TIMERPROC)TimerProc);
...
我如何IDT_TIMER1
获得TimerProc
?idEvent 与此值不匹配,并且 uMsg 始终为 0x110 (WM_TIMER),它是否以某种方式编码,因为 idEvent 就像0x739B
while IDT_TIMER1
is1001
(0x3E9)
解决方案
从文档中弄清楚。
idEvent - Specifies a nonzero timer identifier. If the `hWnd` parameter is NULL, this parameter is ignored.
所以它被忽略了。
像这样修复它
#define IDT_TIMER_1 1001
UINT_PTR Timer1IdEvent;
Timer1IdEvent = SetTimer(hwnd, IDT_TIMER1, 1000, (TIMERPROC)TimerProc);
...
VOID CALLBACK TimerProc(HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime)
{
if( Timer1IdEvent == idEvent) {
MessageBox(NULL, "One second is passed, the timer procedure is called, killing the timer", "Timer Procedure", MB_OK);
KillTimer(hwnd, idEvent);
}
}
推荐阅读
- pandas - Pandas - 计算不包括当前行的组的滚动平均值
- c - 如何检查命令行中的输入是C中的整数?
- arangodb - 在 ArangoDB 中何时使用 RocksDB 以及何时使用 MMFiles 存储引擎?
- json - 无法将大型 JSON 文件上传到 Firebase 实时数据库
- javascript - 在 chrome 扩展中使用现有的 SVM 分类器(用 python 创建)的可能性?
- node.js - HEROKU 和 HAPIJS 错误 R10(启动超时)-> Web 进程未能在启动后 60 秒内绑定到 $PORT
- xcode - 使用 swift 4 发出 GET 请求在游乐场中有效,但在项目中无效
- c - .txt 文件的输入和输出问题
- c - 如何在 c 代码中找到变量/函数的声明?
- excel - 从 Excel 到 Quant mod