定义:
SetTimer( HWND hWnd ,//句柄 用0也行 UINT nIDEvent,//定时器的序号 UINT uElapse,//间隔时间 单位为毫秒 TIMERPROC lpTimerFunc//回调函数 );
KillTimer( HWND hWnd, UINT uIDEvent);
实例一:
#define AUTO_PLAY 1 VOID CALLBACK AutoPlay_TimerProc( HWND hwnd, // handle to window UINT uMsg, // WM_TIMER message UINT idEvent, // timer identifier DWORD dwTime // current system time ) { bool clear_flag=clear_onepair(); if (FALSE==clear_flag) KillTimer(hwnd,AUTO_PLAY); } void CLlk_wgDlg::OnBtnKill() { int slider_val; float show_time; show_time=slider_val=m_slider.GetPos(); show_time/=1000; KillTimer(AUTO_PLAY); CString pTempInfo; pTempInfo.Format((" %5.3f 秒"),show_time); // 浮点数输出5个有效位,其中3个小数位. m_TimerShow.SetWindowText(pTempInfo); SetTimer(AUTO_PLAY,slider_val,AutoPlay_TimerProc); }
实例二:
//本例为windows后台程序 本来想用控制台 结果在控制台环境不能实现定时 //已经测试通过 #include "stdafx.h" #include "resource.h" #include <windows.h> void CALLBACK Timer(HWND hwnd,unsigned int a,unsigned int b,unsigned long c) { MessageBox(0,"hello","test",0);//弹出对话框 } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. unsigned int id=SetTimer(0,1,1000,Timer);//每1秒调用一次回调函数Timer MessageBox(0,"按我就停止","test",0); KillTimer(0,id); return 0; }