zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    haproxy实现负载均衡集群
    docker私有仓库搭建,证书认证,鉴权管理
    dockerhub私有镜像仓库harbor部署
    Delphi用窗体类名创建窗体(需要用到GetClass)
    Delphi中Class of 第二篇
    Delphi中Class of
    Delphi中ADO之初识
    Delphi遍历枚举
    二进制乘除的原理
    Delphi图像处理之图像着色
  • 原文地址:https://www.cnblogs.com/shya/p/2365051.html
Copyright © 2011-2022 走看看