MFC中定时器使用实例

时间:2022-12-16 00:24:55

一:定时器实例

1.在自动生成的对话框模板添加两个Edit Control控件,ID改为的IDC_YMD和IDC_HMS,添加一个Button,默认ID即可

2.头文件中加入

protected:
afx_msg void OnTimer(UINT nIDEvent);

public:
afx_msg void OnBnClickedButton1();
void Fun1();
void Fun2();


3.在cpp中加入

BEGIN_MESSAGE_MAP(CTimerDlg, CDialogEx)
...
ON_WM_TIMER()
...
END_MESSAGE_MAP()
void CTimerDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
SetTimer(1, 1000, NULL);
SetTimer(2, 2000, NULL);
}

void CTimerDlg::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case 1:
Fun1();
break;
case 2:
Fun2();
break;
default:
break;
}
//CTimerDlg::OnTimer(nIDEvent); // 栈溢出
CDialog::OnTimer(nIDEvent);
}
void CTimerDlg::Fun1()
{
CTime time;
CString strTime;
time = CTime::GetCurrentTime();
strTime = time.Format("%H:%M:%S");
SetDlgItemText(IDC_HMS, strTime);
}
void CTimerDlg::Fun2()
{
CTime time;
CString strTime;
time = CTime::GetCurrentTime();
strTime = time.Format("%Y:%m:%d");
SetDlgItemText(IDC_YMD, strTime);
}

4.运行效果:
MFC中定时器使用实例

二:思考 1.
CTimerDlg::OnTimer(nIDEvent); // 栈溢出
当我写成注释这个语句时:提示栈溢出

原因在于:这样相当于一直不停的在运行我的定时器程序,根本跳不出来,肯定会溢出啊!
解决方法:需要重载OnTimer,当处理完我们定义的事件,这样就可以让内部处理其他的流程。

2.对于溢出的解决方法:
栈溢出就是局部变量太大了,超过了编译器的设定的值。在windows上运行一个程序,默认的栈空间是1M,超出就会stack overflow,特别是对于递归函数,它不停的用栈空间,不回收,解决的办法是使用一些数学公式代替递归。就是修改算法。
方法一:
局部变量通过动态分配,分配堆内存,即,new或者malloc来分配堆上面的内存
方法二:
将“项目属性-->链接器-->系统-->堆栈保留大小”设大一点,例如比如16000000,就行了。
方法三:
在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可。
static int a[N];

3.afx_msg void OnTimer(UINT nIDEvent)与afx_msg void OnTimer(UINT_PTR nIDEvent)有什么区别?
1.基本上没有什么区别,可以替换
2.typedef unsigned __int64 UINT_PTR 意思是声明unsigned为兼容64位的新类型,没什么特别
3.众所周知,在32位操作系统里,一个int是4个字节。64位操作系统上,一个int是8个字节。指针的大小也是同样。所以用INT_PTR代替int理论上可以让代码具有更好的移植性,当然也让代码看起来更专业:)

4.time.Format
strTime = time.Format("%H:%M:%S");
%a - abbreviated weekday name.
%A - full weekday name.
%b - abbreviated month name.
%B - full month name.
%c - date and time, as "%a %b %e %H:%M:%S %Y".
%d - zero-padded day of the month as a decimal number [01,31].
%e - space-padded day of the month as a decimal number [ 1,31]; equivalent to%_d.
%H - hour (24-hour clock) as a decimal number [00,23].
%I - hour (12-hour clock) as a decimal number [01,12].
%j - day of the year as a decimal number [001,366].
%m - month as a decimal number [01,12].
%M - minute as a decimal number [00,59].
%L - milliseconds as a decimal number [000, 999].
%p - either AM or PM.
%S - second as a decimal number [00,61].
%U - week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
%w - weekday as a decimal number [0(Sunday),6].
%W - week number of the year (Monday as the first day of the week) as a decimal number [00,53].
%x - date, as "%m/%d/%Y".
%X - time, as "%H:%M:%S".
%y - year without century as a decimal number [00,99].
%Y - year with century as a decimal number.
%Z - time zone offset, such as "-0700".
%% - a literal "%" character.

本文部分引用自下列文章:
https://zhidao.baidu.com/question/322133242.html
http://blog.csdn.net/whatday/article/details/8668039
http://www.jizhuomi.com/software/232.html
http://blog.csdn.net/rogerzhanglijie/article/details/14164823