作者分享初學 VC++ 經驗,製作一個簡單的時鐘程式,適合初學者參考練習。
IDC_HOUR、IDC_MINUTE、IDC_SECOND。CString 變數:m_hours、m_minutes、m_seconds。WM_TIMER 訊息,產生 OnTimer()。CurrentTime() 取得系統時間並更新顯示:void CClockDlg::CurrentTime() {
time_t tCurrentTime;
char szBuffer[256];
tCurrentTime = time((time_t*)NULL);
strftime(szBuffer, sizeof(szBuffer), "%H", localtime(&tCurrentTime));
m_hours = szBuffer;
strftime(szBuffer, sizeof(szBuffer), "%M", localtime(&tCurrentTime));
m_minutes = szBuffer;
strftime(szBuffer, sizeof(szBuffer), "%S", localtime(&tCurrentTime));
m_seconds = szBuffer;
GetDlgItem(IDC_HOUR)->SetWindowText(m_hours);
GetDlgItem(IDC_MINUTE)->SetWindowText(m_minutes);
GetDlgItem(IDC_SECOND)->SetWindowText(m_seconds);
}
OnInitDialog() 的 return TRUE 前安裝計時器:int iInstallResult = SetTimer(1, 1000, NULL);
if (iInstallResult == 0)
MessageBox("fail to install the timer!");
else
CurrentTime();
OnTimer() 中呼叫 CurrentTime()。可為對話框關閉添加淡出動畫,參考原始碼嘗試。