2、创建MFC应用程序——基于对话框,时间计时器

使用计时器更新MFC界面时间,频率1s。

文件——新建项目——MFC应用程序,下一步,选择基于对话框,其他默认,完成。

双击窗体(或者鼠标右键)进入类向导,自动创建Ontimer()函数

void CMFCApplication8Dlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO:  在此添加消息处理程序代码和/或调用默认值
    CTime time = CTime::GetCurrentTime();//获取当前时间
    CString str;
    str.Format(_T("%d年%2d月%2d日 %2d:%2d:%2d"), time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond());
    SetDlgItemText(IDC_EDIT1, str);//在文本框中显示str结果

    CDialogEx::OnTimer(nIDEvent);
}

// CMFCApplication8Dlg 消息处理程序

BOOL CMFCApplication8Dlg::OnInitDialog()中添加

SetTimer(1, 1000, NULL);//计时器1,1000ms触发一次OnTimer()函数

猜你喜欢

转载自www.cnblogs.com/xixixing/p/11872296.html