settimer(SetTimer)
本文目录
SetTimer
一起探讨一下如何使用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()函数中编写汉书,进行相应。如此,就能做出动画。
SetTimer()用法
不知道你的SetTimer是MFC的还是API的,所以都给你列出来MFC中的SetTimerCWnd::SetTimerUINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );Return ValueThe timer identifier of the new timer if the function is successful. An application passes this value to the KillTimer member function to kill the timer. Nonzero if successful; otherwise 0.ParametersnIDEventSpecifies a nonzero timer identifier.nElapseSpecifies the time-out value, in milliseconds.lpfnTimerSpecifies 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.RemarksInstalls a system timer. A time-out value is specified, and every time a time-out occurs, the system posts aWM_TIMER message to the installing application’s message queue or passes the message to an application-defined TimerProc callback function. The lpfnTimer callback function need not be named TimerProc, but it must be defined as follows:void CALLBACK EXPORT TimerProc( HWND hWnd, // handle of CWnd that called SetTimer UINT nMsg, // WM_TIMER UINT nIDEvent // timer identification DWORD dwTime // system time);Timers are a limited global resource; therefore it is important that an application check the value returned by the SetTimer member function to verify that a timer is actually available.Example// This example shows how to use CWnd::SetTimer, CWnd::KillTimer, and how to handle WM_TIMER messages. A timer is set up to send a WM_TIMER message to the main frame window every 2 seconds in OnStartTimer(). OnStopTimer will stop the timer by calling CWnd::KillTimer. OnTimer was set up through class wizard to handle WM_TIMER messages for the main frame window. In this example the PC speaker will beep every 2 seconds.void CMainFrame::OnStartTimer() { m_nTimer = SetTimer(1, 2000, 0);}void CMainFrame::OnStopTimer() { KillTimer(m_nTimer); }void CMainFrame::OnTimer(UINT nIDEvent) { MessageBeep(0xFFFFFFFF); // Beep // Call base class handler. CMDIFrameWnd::OnTimer(nIDEvent);}Windows API中的SetTimerSetTimerThe SetTimer function creates a timer with the specified time-out value. UINT_PTR SetTimer( HWND hWnd, // handle to window UINT_PTR nIDEvent, // timer identifier UINT uElapse, // time-out value TIMERPROC lpTimerFunc // timer procedure);ParametershWnd Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored. nIDEvent Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. uElapse Specifies the time-out value, in milliseconds. lpTimerFunc Pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message’s MSG structure contains the value of the hWnd parameter. Return ValuesIf the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer. If the function succeeds and the hWnd parameter is not NULL, then the return value is a nonzero integer. An application can pass the value of the nIDTimer parameter to the KillTimer function to destroy the timer.If the function fails to create a timer, the return value is zero. To get extended error information, call GetLastError.RemarksAn application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter. The timer identifier, nIDEvent, is specific to the associated window. Another window can have its own timer which has the same identifier as a timer owned by another window. The timers are distinct.
c#中settimer的用法
不了解c# vc中settimer用法:settimer( HWND hWnd,UINT_PTR nIDEvent,UINT uElapse,TIMERPROC lpTimerFunc);其中hWnd默认为当前窗口句柄,_PTR为计时器编号,ublapse 为时间间隔,lpTimerFunc为一个时间间隔所执行的函数如为null执行Ontimer();例如:settimer(1,1000,null);结束可以用killtimer(1);结束计时器
settimer 怎么用
MFC中的定时器大致分为4个步骤:1.在需要添加定时器的类名上右键选择Add Windows Message Handler,然后添加相应消息WM_TIMER.2.在程序中需要启动计时器的地方UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)( HWND, UINT, UINT, DWORD) ); 第一个参数为定时器id,第二个参数为间隔时间,以毫秒为单位,第三个参数为处理过程入口地址,一般为NULL;例如SetTimer(1,500,NULL);3.在CXXXDlg::OnTimer(UINT nIDEvent);函数中添加处理代码4.在需要销毁定时器的地方BOOL KillTimer( int nIDEvent ); //参数为定义器的id,需与SetTimer中一致例如KillTimer(1);
关于SetTimer的问题,求大神!
是在AfxMessageBox弹出的消息框的消息循环里,又处理了WM_TIMER,然后又弹出对话框的结果。你可以把MFC调成静态链接,然后调试,然后让它弹出8个MessageBox,不要点掉,在VC里暂停进程,之后VC可能会停在某个类似汇编代码的地方,此时找调用堆栈窗口(call stack),检查调用栈,你可以看到好几个OnTimer好几个AfxMessageBox在调用栈里。就是这么回事:AfxMessageBox还没返回,结果又开始处理WM_TIMER了(直接在运行AfxMessageBox的线程(消息循环)上开始处理消息AfxMessageBox之所以可以在你点掉窗口之前不返回,不返回的同时还能让你后面那些窗口不会没有响应,就是这个原理:它内部运行了一个消息循环。
更多文章:
jupyter notebook无法跳转浏览器(jupyter该怎么用)
2024年7月8日 02:52
concerta(Cash Cash的《Concerta》 歌词)
2024年7月10日 13:29
looking forward to(look forward to 什么意思什么用法)
2024年7月22日 05:35
简述重载和重写的相同点(Java中方法重载和方法重写的异同)
2024年7月13日 18:14
表单大师怎么在手机上导出数据(问卷调查满意度结果分析表怎么做)
2024年7月3日 17:52
打开activex控件异常(读卡器读卡时显示调用ActiveX控件出错:对象不支持此属性或方法)
2024年9月27日 18:15
js时钟代码(javascript小时钟t=setTimeout(’startTime()’,500)为什么隔500毫秒就刷新)
2024年7月12日 04:25
filezilla文件名显示乱码(文件名出现奇怪的乱码,求解决方法)
2024年7月19日 07:30
新手弹贝斯到哪个阶段再练slap比较合适?请问bass中的slap是什么意思
2024年7月4日 08:35
duck是什么意思(为什么I forget to duck不是翻译为我忘了要鸭了duck有什么特殊的含义)
2024年5月26日 08:54