1.在类中OnInitDialog()函数中添加定时器,并设置字体
BOOL CServer::OnInitDialog()
{
<span style="white-space:pre"> </span>CDialogEx::OnInitDialog();
<span style="white-space:pre"> </span>//显示实时时间
<span style="white-space:pre"> </span>m_timeFont.CreatePointFont(140,_T("宋体"),NULL);
<span style="white-space:pre"> </span>SetTimer(1, 1000, NULL);
<span style="white-space:pre"> </span>return TRUE;
}
2.定时器响应函数写代码 在类的属性中找到消息响应函数WM_TIMER并添加即可</p><p><pre name="code" class="cpp">void CServer::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CTime tm = CTime::GetCurrentTime();
CString str = tm.Format("%Y年%m月%d日 %X");
GetDlgItem(IDC_TIME)->SetFont(&m_timeFont);
GetDlgItem(IDC_TIME)->SetWindowText(str);
CDialogEx::OnTimer(nIDEvent);
}
3.显示效果