在Vs2022下,应用VC++控制台程序,SetTimer定时器的应用,在很多的介绍例程当中,都使用了
SetTimer(NULL, TimerID, peried, TimerProc);
但是当这个语句直接在VS2022内使用时,会出现【严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2664 “UINT_PTR SetTimer(HWND,UINT_PTR,UINT,TIMERPROC)”: 无法将参数 4 从“void (__cdecl *)(HWND,UINT,UINT,DWORD)”转换为“TIMERPROC” ConsoleApplication2 C:\Users\47452\source\repos\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp 276 】
这个地方需要进行类型强制转换:SetTimer(NULL,TimerID,peried, (TIMERPROC)TimerProc);
2021.12.10 控制台应用定时器SetTimer时遇到问题,予以为记。J
以下为完整的程序,可以直接在VS2022,VC++控制台程序下应用。
#include <Windows.h>
#include <iostream>
//控制台消息响应
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
::MessageBeep(0);//Beep叫
cout << " it's timer message " << endl; //打印字符
}
int main()
{
//=============================================
//消息主循环,定时器
int TimerID = 1;//Timer的ID是1
int peried = 60000;//1000;//Timer的间隔是 1000ms
//设置Timer
SetTimer(NULL,TimerID,peried, (TIMERPROC)TimerProc);
::MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_TIMER) //定时器消息
{
::DispatchMessage(&msg);
}
}
}