C++定时器用法(已经封装成类)

编译环境

以下代码在VC7下测试通过。

示例代码

//
//ShyaTimer.h
//作者:砍柴人
//日期:2012-2-23
//
//
#include <Windows.h>
#include <stdio.h>
#include <Mmsystem.h.>
#pragma comment(lib,"winmm.lib")

/************************************************************************/
/* 事件处理函数,直接在里面写要处理的代码 */
/************************************************************************/
void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 );
namespace Shya
{
class ShyaTimer
{
protected:
MMRESULT nIDTimerEvent;
public:
UINT uDelay;
ShyaTimer()
{
uDelay=1000;
}
ShyaTimer(UINT delay)
{
uDelay=delay;
}
void SetTimerDelay(UINT delay)
{
uDelay=delay;
}
UINT GetTimerDelay()
{
return uDelay;
}
bool Start()
{
nIDTimerEvent=timeSetEvent(uDelay,0,(LPTIMECALLBACK)TimeProc,0,(UINT)TIME_PERIODIC);
if (nIDTimerEvent==0)
{
printf("启动时钟失败!\n" );
return false;
}
return true;
}
~ShyaTimer()
{
Release();
}
void Release()
{
timeKillEvent(nIDTimerEvent);
}
};
}

使用方法

1、添加头文件

2、包含头文件,引用SHYA命名空间

3、实现处理函数:TimeProc

4、调用ShyaTimer类的方法;

使用示例代码

#include "stdafx.h"
#include "shyatimer.h"
using namespace Shya;


void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 )
{
printf("时钟测试\n");
}

int main(int argc, char *argv[])
{
ShyaTimer *st=new ShyaTimer();
st->SetTimerDelay(2000);
st->Start();
printf("%d",st->GetTimerDelay());
getchar();
st->Release();
return 0;
}



转载于:https://www.cnblogs.com/shya/archive/2012/02/23/2365051.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值