zoukankan      html  css  js  c++  java
  • (转载)VC定时器使用回调函数

    回调函数的使用。

        如果不想使用窗体的WM_TIMER消息函数处理,可以使用回调函数来取代,增加一个回调函数。

        首先,定义一个回调函数,回调函数的定义必须按照如下格式。

    void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime );


    我的实现函数如下:

    void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime )

    {

    AfxMessageBox("Timer is running!");//定时器时间到,强出一对话框,表明定时器已经运行。

    }


    SetTimer(1,1000,(TIMERPROC)TimerProc);//用回调函数处理,此时对话框的消息处理函数不再处理。

       如果是在主线程中调用其他类的回调函数:

        static void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime );

        

    void CALLBACK CBattery::TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime )

    {

    AfxMessageBox("Timer is running!");//定时器时间到,强出一对话框,表明定时器已经运行。

    }

     SetTimer(1,1000,(TIMERPROC)CBattery::TimerProc);

    通常,我们在使用定时器时,只用到三个参数,即
    UINT CWnd::SetTimer(
    UINT nIDEvent,
    UINT nElapse,
    void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD)
    );

    其实,这个函数只是MFC对API的封装,其实现函数为:

    _AFXWIN_INLINE UINT CWnd::SetTimer(UINT nIDEvent, UINT nElapse,

    void (CALLBACK* lpfnTimer)(HWND, UINT, UINT, DWORD))

    {
    ASSERT(::IsWindow(m_hWnd));
     return ::SetTimer(m_hWnd, nIDEvent, nElapse,(TIMERPROC)lpfnTimer);
    }

    由此可见,CWnd::SetTimer只是将API函数SetTimer的第一个参数设置成它自己的句柄而已。

  • 相关阅读:
    layui 相关知识
    ideal debug 启动不起来
    删除命令 rm -rf maven-project-jxcg.zip 解压 unzip maven-project-jxcg.zip
    vsb 配置
    cmd dos
    switch
    Element UI 框架搭建
    mysql 远程连接设置
    YApi可视化接口管理平台 接口
    报403错误
  • 原文地址:https://www.cnblogs.com/ccmfc/p/1844827.html
Copyright © 2011-2022 走看看