OnTimer&SetTimer使用方法(MFC)

MFC中使用Timer事件处理机制,

1、在头文件中声明,afx_msg void OnTimer(UINT nIDEvent);

2、在消息映射里追加,ON_WM_TIMER()(忘记追加消息将不会响应)

3、在使用时开始Timer,例如:SetTimer(44, 1000, NULL);原型为SetTimer(UINT_PTR nIDEvent, UINT nElapse,
        void (CALLBACK* lpfnTimer)(HWND, UINT, UINT_PTR, DWORD)).在MFC中,只需要定义参数1事件名(nIDEvent),参数2时间(nElapse)。参数3可以设置为NULL。

4、添加消息响应函数,

void CMFCTestDlg::OnTimer(UINT nIDEvent)
{
    switch(nIDEvent)   
    {   
    case 44:   ///处理ID为24的定时器
        //do something when event coming.
    break;   

    }   

    CDialogEx::OnTimer(nIDEvent);  
}

5、使用结束时结束Event。KillTimer(44);

你可能感兴趣的:(C++)