Windows Console 程序中定时器的使用

本程序使用定时器方法,设置一个1000ms的定时器(SetTimer),然后捕捉该消息,然后调用回调函数TimerProc,在该函数中输出DEF,注意,SetTimer可以直接写回调函数地址,不必捕捉消息。

这里给出的是捕捉消息的版本。

//************************************************************************************    
//本程序使用定时器方法,设置一个1000ms的定时器(SetTimer),然后捕捉该消息,然后调用回调函数    
//     TimerProc,在该函数中输出DEF,注意,SetTimer可以直接写回调函数地址,不必捕捉消息。       
//************************************************************************************    



#include "stdafx.h"    
#include <windows.h>    


void TimerProc()
{

    static DWORD tick = 0;
    static DWORD tmp = GetTickCount();

    tick = GetTickCount() - tmp;
    tmp = GetTickCount();
    printf(" TimerProc________________def %d\n", tick);
}


int main()
{

    SetTimer(NULL, 0, 1000, NULL); //设置一个定时器,定时器的回调函数为0,仅产生定时器消息,在main函数里捕捉该消息 
    MSG msg;
    BOOL bRet;

    while (1)
    //该循环捕捉定时器消息,并且防止main函数退出    
    {
        bRet = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
        if (bRet ==  - 1)
        {
            // handle the error and possibly exit
        }
        else if (bRet && msg.message == WM_TIMER)
        {
            TimerProc();
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }


}
原文地址:https://www.cnblogs.com/xiangtailiang/p/2444632.html