本文列举两个例子进行说明。

例 1,  不使用回调函数,是按照时间到后 去响应一个自定义的函数

#include<iostream>
#include<Windows.h>

using namespace std;

void resp_fun()
{
    cout<<"定时一次!\n";
}

int main()
{
   DOWRD dwTimerId;
   dwTimerId = SetTimer(NULL,1,1000,NULL);
   MSG msg;
   unsigned int i = 0;
   while(GetMessage(&msg, NULL, 0, 0))
   {
       if(i > 9)
       {
             KillTimer(NULL, dwTimerId);
             dwTimerId=SetTimer(NULL,1,1000,NULL);  
             break;
       }
       else if(msg.message == WM_TIMER)
       {
             Resp_fun();
       }
       ++i;
   }
   return 0;
}

例 2,使用windows消息循环机制

#include<iostream>
#include<Windows.h>

using namespace std;

#define ID_TIMER 0
void CALLBACK printSTR(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTimer)
{
    cout<<"hello world!\n";
}

int main()
{
    int iId;
     MSG msg;
     
    iId = SetTimer(NULL, ID_TIMER, 1000, printSTR);
     
    while(GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }
     
    KillTimer(NULL, iId);
    return 0;
}


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐