MFC 中的 Timer函数

首页 / MFC入门教程 / MFC 中的 Timer函数

MFC计时器既没有按钮来表示它,也没有类,要创建计时器,只需调用CWnd::SetTimer()方法,此函数调用为您的应用程序创建一个计时器,像其他控件一样,计时器使用标识符。

让无涯教程创建一个新的基于MFC对话框的应用程序。

步骤1 - 删除标题并将其ID设置为IDC_STATIC_TXT

步骤2 - 为文本控件添加value变量。

Timer

步骤3 - 转到解决方案中的类视图。

步骤4 - 单击CMFCTimeDlg类。

步骤5 - 在"Properties"窗口中,单击"Messages"按钮。

Timer

步骤6 - 单击WM_TIMER字段,然后单击其组合框的箭头,选择 OnTimer并实现该事件。

void CMFCTimerDlg::OnTimer(UINT_PTR nIDEvent) { 
   //TODO: Add your message handler code here and/or call default 
   CTime CurrentTime = CTime::GetCurrentTime();  
	
   int iHours = CurrentTime.GetHour(); 
   int iMinutes = CurrentTime.GetMinute(); 
   int iSeconds = CurrentTime.GetSecond(); 
   CString strHours, strMinutes, strSeconds;  
 
   if (iHours < 10) 
      strHours.Format(_T("0%d"), iHours); 
   else 
      strHours.Format(_T("%d"), iHours);  
 
   if (iMinutes < 10) 
      strMinutes.Format(_T("0%d"), iMinutes); 
   else 
      strMinutes.Format(_T("%d"), iMinutes);  
   
   if (iSeconds < 10) 
      strSeconds.Format(_T("0%d"), iSeconds); 
   else 
      strSeconds.Format(_T("%d"), iSeconds);  
 
   m_strTimer.Format(_T("%s:%s:%s"), strHours, strMinutes, strSeconds); 
   
   UpdateData(FALSE); 
   CDialogEx::OnTimer(nIDEvent); 
}

步骤7 - 编译并执行上述代码后,您将看到以下输出。

Timer

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学大数据 -〔李智慧〕

性能测试实战30讲 -〔高楼〕

深入浅出云计算 -〔何恺铎〕

编译原理实战课 -〔宫文学〕

Vim 实用技巧必知必会 -〔吴咏炜〕

用户体验设计实战课 -〔相辉〕

成为AI产品经理 -〔刘海丰〕

分布式金融架构课 -〔任杰〕

性能优化高手课 -〔尉刚强〕

好记忆不如烂笔头。留下您的足迹吧 :)