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);
	
}

运行程序

源码下载

相关推荐
端平入洛1 天前
auto有时不auto
c++
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马2 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝2 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc2 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼2 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx2 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX2 天前
020-C++之unordered容器
数据结构·c++
会编程的土豆2 天前
2.25 做题
数据结构·c++·算法