/////////////////////////////////////////// MyTimer.h: interface for the CMyTimer class.
#if !defined(AFX_MYTIMER_H__D97674D1_B221_49CD_9637_4CBA8C3180CE__INCLUDED_)
#define AFX_MYTIMER_H__D97674D1_B221_49CD_9637_4CBA8C3180CE__INCLUDED_
#i nclude <afxtempl.h>
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyTimer;
typedef CMap<UINT,UINT,CMyTimer*,CMyTimer*> CTimerMap;
class CMyTimer
{
public:
//设置定时器,nElapse表示时间间隔,sz表示要提示的内容
void SetMyTimer(UINT nElapse,CString sz);
//销毁该实例的定时器
void KillMyTimer();
//保存该实例的定时器标志值
UINT m_nTimerID;
//静态数据成员要提示的内容
CString szContent;
//静态数据成员,映射表类,用于保存所有的定时器信息
static CTimerMap m_sTimeMap;
//静态成员函数,用于处理定时器的消息
static void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
CMyTimer();
virtual ~CMyTimer();
};
#endif
///////////////////////////////////// MyTimer.cpp: implementation of the CMyTimer class.
#i nclude "stdafx.h"
#i nclude "TimerDemo.h"
#i nclude "MyTimer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CTimerMap CMyTimer::m_sTimeMap;
CMyTimer::CMyTimer()
{
m_nTimerID = 0;
}
CMyTimer::~CMyTimer()
{}
void CALLBACK CMyTimer::MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
CString sz;
sz.Format("%d号定时器:%s",idEvent,m_sTimeMap[idEvent]->szContent);
AfxMessageBox(sz);
}
void CMyTimer::SetMyTimer(UINT nElapse,CString sz)
{
szContent = sz;
m_nTimerID = SetTimer(NULL,NULL,nElapse,MyTimerProc);
m_sTimeMap[m_nTimerID] = this;
}
void CMyTimer::KillMyTimer()
{
KillTimer(NULL,m_nTimerID);
m_sTimeMap.RemoveKey(m_nTimerID);
}