如何使用SetTimer MFC
2008-09-05 17:41
330 查看
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 TimerProccallback 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::OnTimerafx_msg void OnTimer( UINT nIDEvent );ontimer()是响应CWnd对象产生的WM_Timer消息。nIDEvent表示要响应TIMER事件的ID。 二、Timer事件的使用: 由以上的分析,我们应该很清楚,如何来使用Timer事件。假定我们在视图上画一个渐变的动画。我们首先在菜单栏上添加一个菜单项,给这个菜单添加命令响应: pView->SetTimer(1,1000,NULL);//pView是视图类的指针,这里是在视图类当中设置一个定时器。 添加完毕,再给视图类添加一个WM_Timer事件的相应。在OnTimer()函数中编写汉书,进行相应。 如此,就能做出动画。 |
#include <wtypes.h>
#include <stdio.h>
#include <iostream>
#include <windows.h> /* For the CreateThread prototype */
using namespace std;
int count=0;
VOID CALLBACK timer(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
cout<<"hello "<<count++<<endl;
return;
}
void main()
{
MSG msg;
cout<<"intput a integer"<<endl;
int send = SetTimer(NULL, 0, 1000, (TIMERPROC)timer);
if(send) {
cout<<"success TimeID: "<<send<<endl;
}
while (GetMessage (&msg, NULL, 0, 0))
{
if(msg.message == WM_TIMER){
cout<<"WM_TIMER" << endl;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(NULL, send);
}
相关文章推荐
- 如何使用SetTimer MFC 够详细
- 如何使用SetTimer MFC
- 如何使用SetTimer MFC 够详细
- 如何使用SetTimer MFC 够详细
- 如何使用SetTimer MFC
- mfc settimer使用
- SetWaitableTimer使用方法之:如何使用setwaitabletimer每个1秒发送一次信号
- MFC定时器使用--SetTimer && KillTimer && 进度条CProgressCtrl
- MFC ListControl如何使用
- MFC简单皮肤颜色绘制(SetDialogBkColor不再被支持,看 WM_CTLCOLOR消息如何百变控件颜色)
- 如何自动化 Excel 使用 MFC 和工作表函数
- 猎豹MFC--定时器SetTimer KillTimer
- duilib篇 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)
- 使用VS2008开发MFC,如何支持中文输入? .
- VC++非MFC项目中如何使用TRACE宏
- 当打开一个文档时,MFC如何选择所使用的文档模板对象?
- 您想知道如何在Access 2003中使用ON UPDATE CASCADE/ON DELETE CASCADE,ON UPDATE SET NULL/ ON DELETE SET NULL吗?
- 如何在MFC程序中使用Open Inventor
- 如何使用MFC编写自定义UI界面【附高仿QQ 2014登陆界面范例程序】
- 如何嵌入并自动使用 MFC 的 Word 文档