如何使用SetTimer MFC

来源:互联网 发布:怎样注册开淘宝网店 编辑:程序博客网 时间:2024/04/30 20:15
Timer事件,即定时器事件,是在游戏编程中,经常使用的一个事件。借助它可以产生定时执行动作的效果。这篇文章,就和大家一起探讨一下如何使用SetTimer()函数。

            1、SetTimer定义在那里?

             SetTimer表示的是定义个定时器。根据定义指定的窗口,在指定的窗口(CWnd)中实现OnTimer事件,这样,就可以相应事件了。

SetTimer有两个函数。一个是全局的函数::SetTimer()

UINT SetTimer(
       HWND hWnd,               // handle of window for timer messages
       UINT nIDEvent,           // timer identifier
       UINT uElapse,            // time-out value
       TIMERPROC lpTimerFunc        // address of timer procedure
);

其中hWnd 是指向CWnd的指针,即处理Timer事件的窗口类。说道窗口类(CWnd),我们有必要来看一下CWnd的继承情况:CWnd有以下子类:CFrameWnd,CDialog,CView,CControlBar等类。这也意味这些类中都可以定义SetTimer事件。

同时,SetTimer()在CWnd中也有定义,即SetTimer()是CWnd的一个成员函数。CWnd的子类可以调用该函数,来设置触发器。

UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );

参数含义:

nIDEvent:是指设置这个定时器的iD,即身份标志,这样在OnTimer()事件中,才能根据不同的定时器,来做不同的事件响应。这个ID是一个无符号的整型。

nElapse

是指时间延迟。单位是毫秒。这意味着,每隔nElapse毫秒系统调用一次Ontimer()。

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

Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application’s message queue and handled by the CWnd object。

意思是,指定应用程序提供的TimerProc回调函数的地址,来处里这个Timer事件。如果是NULL,处理这个Timer事件的定义这个Timer的CWnd对象。他将WM_TIMER消息传递给这个对象,通过实现这个对象的OnTimer()事件来处理这个Timer事件。

所以,一般情况下,我们将这个值设为NULL,有设置该定时器的对象中的OnTimer()函数来处理这个事件。

同样的,我们再看看KillTimer()和OnTimer()的定义:

KillTimer同SetTimer()一样,他也有两个,一个是全局的::KillTimer(),另一个是CWnd的一个函数。他的声明如下:

//全局函数

BOOL KillTimer(
     HWND hWnd,       // handle of window that installed timer
     UINT uIDEvent      // timer identifier
);

//CWnd函数

BOOL KillTimer( int nIDEvent );

这两个函数表示的意思是将iD为nIDEVENT的定时器移走。使其不再作用。其用法如同SetTimer()一样。

再看看OnTimer()

CWnd::OnTimer  

afx_msg void OnTimer( UINT nIDEvent );

ontimer()是响应CWnd对象产生的WM_Timer消息。nIDEvent表示要响应TIMER事件的ID。

二、Timer事件的使用:

由以上的分析,我们应该很清楚,如何来使用Timer事件。假定我们在视图上画一个渐变的动画。我们首先在菜单栏上添加一个菜单项,给这个菜单添加命令响应:

pView->SetTimer(1,1000,NULL);//pView是视图类的指针,这里是在视图类当中设置一个定时器。

添加完毕,再给视图类添加一个WM_Timer事件的相应。在OnTimer()函数中编写汉书,进行相应。

如此,就能做出动画。

  1. #include "stdafx.h"  
  2. #include <wtypes.h>  
  3. #include <stdio.h>  
  4. #include   <iostream>     
  5. #include   <windows.h>   /*   For   the   CreateThread   prototype   */      
  6.   
  7. using   namespace   std;      
  8.   
  9.        
  10.   
  11. int   count=0;      
  12.   
  13.        
  14.   
  15. VOID   CALLBACK   timer(      
  16.   
  17.      HWND   hwnd,           //   handle   of   window   for   timer   messages      
  18.   
  19.      UINT   uMsg,           //   WM_TIMER   message      
  20.   
  21.      UINT   idEvent,     //   timer   identifier      
  22.   
  23.      DWORD   dwTime       //   current   system   time      
  24.   
  25. )      
  26.   
  27. {      
  28.   
  29.   cout<<"hello   "<<count++<<endl;      
  30.   
  31.   return;      
  32.   
  33. }      
  34.   
  35.   
  36.   
  37.   
  38.   
  39. void   main()      
  40.   
  41. {      
  42.   
  43.     MSG   msg;      
  44.   
  45.     cout<<"intput   a   integer"<<endl;      
  46.   
  47.     int   send = SetTimer(NULL, 0, 1000, (TIMERPROC)timer);      
  48.   
  49.     if(send) {   
  50.   
  51.         cout<<"success TimeID: "<<send<<endl;      
  52.   
  53.     }   
  54.   
  55.   
  56.   
  57.        
  58.   
  59.     while   (GetMessage   (&msg,   NULL,   0,   0))     
  60.   
  61.     {      
  62.   
  63.         if(msg.message == WM_TIMER){   
  64.   
  65.             cout<<"WM_TIMER" << endl;   
  66.   
  67.         }   
  68.   
  69.         TranslateMessage(&msg);      
  70.   
  71.         DispatchMessage(&msg);      
  72.   
  73.     }      
  74.   
  75.        
  76.   
  77.     KillTimer(NULL, send);      
  78.   
  79.        
  80.   
  81.  }  
原创粉丝点击