vc高精度多媒体定时器的使用方法(MFC)

3 篇文章 0 订阅
在VC编程中,用SetTimer可以定义一个定时器,到时间了,就响应OnTimer消息,但这种定时器精度太低了。如果需要精度更高一些的定时器(精确到1ms),可以使用下面的高精度多媒体定时器进行代码优化,可以达到毫秒级的精度,而且使用方便。先要包含头文件"mmsystem.h"和库文件"winmm.lib"。


vc高精度多媒体定时器的使用方法如下:


复制内容到剪贴板 程序代码


#include "mmsystem.h"  //head file


#pragma comment(lib,"winmm")  //lib file


const    int        timePeriod = 2;
const    int        timeRes = 1 ;


/*******************MMTimer fuction********************************\
   CreateTimer :   create a Multimedia timer
   DestroyTimer:   destroy a Multimedia timer
   TimerHandler:   the actual timer handler procedure
\******************************************************************/


/******************************************************************\
  function
    name : CreateTimer
    desc : create a realtime timer
  argument
    void
  ret code
    [HANDLE] ,the handle of the timer
\******************************************************************/
UINT CMyTimer::CreateTimer()
{
    //create the timer
    
    // Create a periodic timer
    timeBeginPeriod(timeRes);
    timerID = timeSetEvent(
        timePeriod,
        timeRes, 
        TimerHandler,
        (DWORD)this,
        TIME_PERIODIC);
    
    return timerID;
}


/******************************************************************\
  function
    name : DestroyTimer
    desc : destroy the timer created by calling CreateTimer
  argument
    void
  ret code
    void
\******************************************************************/
void CMyTimer::DestroyTimer()
{


    if ( bRun )
    {
        timeKillEvent(timerID);
        timeEndPeriod(timeRes);


        bRun = FALSE;
    }




}


/******************************************************************\
  function
    name : TimerHandler
    desc : timer procedure called when the the timer signaled
  argument
    dwUser,[in],user para data
  ret code
    void
\******************************************************************/


void CALLBACK CMyTimer::TimerHandler(UINT id, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
    CMyTimer* pThis = (CMyTimer*)dwUser;


}






使用多媒体定时器timeSetEvent()函数,该函数定时精度为ms级。利用该函数可以实现周期性的函数调用。函数的原型如下: 
       MMRESULT timeSetEvent( UINT uDelay, 
                               UINT uResolution, 
                               LPTIMECALLBACK lpTimeProc, 
                               WORD dwUser, 
                               UINT fuEvent )


  该函数设置一个定时回调事件,此事件可以是一个一次性事件或周期性事件。事件一旦被激活,便调用指定的回调函数, 成功后返回事件的标识符代码,否则返回NULL。函数的参数说明如下:


       uDelay:以毫秒指定事件的周期。
       Uresolution:以毫秒指定延时的精度,数值越小定时器事件分辨率越高。缺省值为1ms。
       LpTimeProc:指向一个回调函数。
       DwUser:存放用户提供的回调数据。
       FuEvent:指定定时器事件类型:
       TIME_ONESHOT:uDelay毫秒后只产生一次事件
       TIME_PERIODIC :每隔uDelay毫秒周期性地产生事件。
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值