添加定时器

  1. String Table中添加字符串资源IDS_TIMER,时钟
  2. indicators[]中添加IDS_TIMER
  3. SetTimer(1, 1000, NULL)
  4. 添加消息响应函数OnTimer()

代码片段如下:

static UINT indicators[] =  
{
	ID_SEPARATOR,           // status line indicator
//	ID_INDICATOR_CAPS,  //Caps Lock键状态指示器
//	ID_INDICATOR_NUM,  //Num Lock键状态指示器
//	ID_INDICATOR_SCRL,  //Scroll Lock键(滚动锁定键一般不用)状态指示器
	IDS_TIMER
}; //添加或减少像一个的字符串资源ID,即添加或减少一个状态栏上的窗格
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
   ...
   SetTimer(1, 1000, NULL);//添加定时器,时间间隔为1000ms 
   return 0;
}
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
	CTime t = CTime::GetCurrentTime();  //得到当前时间
	CString str = t.Format("%H:%M:%S");  //表示时间格式的字符串

	CClientDC dc(this);
	CSize sz = dc.GetTextExtent(str);  //获得时间文本的宽度

	int index = 0;
	index = m_wndStatusBar.CommandToIndex(IDS_TIMER);//得到IDS_TIMER的窗格索引

	m_wndStatusBar.SetPaneInfo(index, IDS_TIMER, SBPS_NORMAL, sz.cx); //为指定的窗格设置新的ID、样式、宽度

	m_wndStatusBar.SetPaneText(index, str);  //显示到状态栏上


	CFrameWnd::OnTimer(nIDEvent);
}

第3步添加消息响应函数OnTimer时遇到Error “重载函数OnTimer已存在”,原因不详。可手动添加OnTimer.
在这里插入图片描述
手动添加OnTimer步骤:

  1. MainFrm.h文件中添加消息响应函数定义:afx_msg void OnTimer(UINT_PTR nIDEvent);
  2. MainFrm.cpp文件中添加消息映射宏:ON_WM_TIMER()
  3. MainFrm.cpp文件中添加响应函数:
    void CMainFrame::OnTimer(UINT_PTR nIDEvent){ CFrameWnd::OnTimer(nIDEvent);}
发布了38 篇原创文章 · 获赞 1 · 访问量 1870

猜你喜欢

转载自blog.csdn.net/qq_36633275/article/details/104002061