VC++ MFC 定时器应用
2012-06-28 08:08
281 查看
MFC定时器应用
一、启动定时器void CP423Dlg::OnStarttimer()
{
// TODO: Add your control notification handler code here
// m_nTimer = SetTimer(1, 5000, TimerProc); ① 如果启动成功的话,m_nTimer = 1
m_nTimer = SetTimer(2, 5000, NULL); ② 如果启动成功的话,m_nTimer = 2
}
定时器处理函数
有两种类型,且只能选择其中一种,两个函数不能同时用
① 定义全局函数
1)在.h中进行声明:
void CALLBACK EXPORT TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
);
2)在.cpp中实现:
void CALLBACK EXPORT TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
{
if (nIDEvent==1)
{
theApp.m_pMainWnd->MessageBox("abc");
}
}
② 消息处理函数
添加消息WM_Timer处理函数
void CP423Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
MessageBox("ontimer");
CDialog::OnTimer(nIDEvent);
}
二、停止定时器
void CP423Dlg::OnStoptimer()
{
// TODO: Add your control notification handler code here
KillTimer(m_nTimer);
}
定时器说明
CWnd::SetTimer
UINT SetTimer( UINT nIDEvent, UINT nElapse, void(CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
Return Value
The 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.
Parameters
nIDEvent
Specifies a nonzero timer identifier.
nElapse
Specifies the time-out value, in milliseconds.
lpfnTimer
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.
Remarks
Installs a system timer. A time-out value is specified, and every time a time-out occurs, the system posts a WM_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); }
相关文章推荐
- MFC定时器的应用
- VC/MFC关于Http协议的应用总结
- VC++MFC中的定时器使用方法
- MFC定时器的应用
- MFC定时器的应用
- VC其它- 在MFC类中各种类的指针的获取和应用
- MFC定时器的应用
- VC++ VS2010 MFC配合串口控件MSCOMM 的实际应用 - 打码机上位机软件 Demo 含例程
- VC中定时器的应用
- 整理一些需要用到的代码——VC++__MFC窗口的应用
- VC++中定时器SetTimer函数的应用总结
- VC-定时器应用
- (转载)用vs2010开发基于VC++的MFC 串口通信一*****两台电脑同一个串口号之间的通信
- MFC树控件应用实例
- Linux的应用层定时器实现方法
- (VC2005)MFC中添加控件的成员变量.
- MFC updatedata在线程中不能应用的解决方法
- vc之pragma宏的简单应用
- VC|MFC内存不能为"read",内存不能为 "written" 分析
- vc mfc按钮的创建