MFC多媒体定时器实例(源码下载)

用MFC多媒体定时器做一个每1秒钟加一次的计时器,点开始计时按钮开始计时,点关闭计时按钮关闭计时。

1、在库文件Med_timeDlg.h文件中添加代码

cpp 复制代码
class CMed_timeDlg : public CDialog
{
// Construction
public:
	CMed_timeDlg(CWnd* pParent = NULL);	// standard constructor
    UINT timerID;//自己添加的定时器ID变量
	UINT timerID1;//自己添加的定时器ID变量
	void DestroyTimer();//自己声明的销毁定时器函数
	UINT CreateTimer();//自己声明的创建定时器函数
	void OnTimer(UINT nIDEvent);
static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);//定时器回调函数
}

2、在项目对话框Med_timeDlg.cpp文件中添加代码

cpp 复制代码
UINT CMed_timeDlg::CreateTimer()
{
	timeBeginPeriod(1);//设置定时器设备的最小时间分辨率
	timerID = timeSetEvent(1000, 1, TimeProc, (DWORD)this, TIME_PERIODIC);

	return timerID;//返回定时器ID

// timeSetEvent(UINT uDelay, UINT uResolution, LPTIMECALLBACK IP TimeProc, DWORD_PTR dwUser, UINT fuEvent);
//创建并初始化定时器事件,定时器回调函数入口地址
//uDelay:定时器触发时间间隔,以毫秒为单位
//uResolution:定时器设备精度,以毫秒为单位,,默认为1ms
//LpTimeProc:定时器出发时间的回调函数的地址
//dwUser:传递给回调函数的数据
//fuEvent:定时类型,TIME_ONSHOT表示uDelay毫秒后只产生一次事件,TIME_PERIOFIC表示每隔uDelay毫秒周期性的产生事件

}


void CMed_timeDlg::DestroyTimer()
{
	timeKillEvent(timerID);//销毁定时器
	timeEndPeriod(1);//清除上次调用 timeBeginPeriod 函数时指定的最小计时器分辨率
}



void CALLBACK CMed_timeDlg::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
	CMed_timeDlg* time_dlg = (CMed_timeDlg*)dwUser;
	time_dlg->OnTimer(uID );
 //uID:多媒体定时器的ID,ID值由timeSetEvent创建定时器事件时返回
 //uMsg:保留 不使用
 //dwUser:由timeSetEvent传递的用户数据
 //dw1,dw2:保留 不使用
}



void CMed_timeDlg::OnBUTTONstart() 
{
	CreateTimer();
	
}

void CMed_timeDlg::OnBUTTONstop() 
{
	DestroyTimer();	
}

	int i=0;
void CMed_timeDlg::OnTimer(UINT nIDEvent)
{
	CString str;
	i++;
	str.Format("%d",i);
		SetDlgItemText(IDC_STATIC1,str);
	
}

运行程序

源码下载

相关推荐
clint4562 天前
C++进阶(1)——前景提要
c++
夜悊2 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0013 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you3 天前
constexpr函数
c++
凡人叶枫3 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫3 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss3 天前
BRpc使用
c++·rpc
-森屿安年-3 天前
63. 不同路径 II
c++·算法·动态规划