在mfc的界面中添加動態時間:系統時間(動態),軟件運行時間(動態)
先在.h頭文件中定義線程(可用定時器OnTime來替換線程);
public:
static DWORD WINAPI threadFuncTime(LPVOID threadTime);//時間線程
HANDLE hThreadTime;//柄
DWORD ThreadIDTime;//ID
其次在“資源視圖”點開工程項目,在Dialog裏面找到對應的DIALOG,“工具箱”點擊“Static Text”,拖出一個靜態框並把ID改成IDC_Time
然後往.cpp文件中 DWORD XXXDlg::threadFuncTime(LPVOID threadTime) 函數添加代碼
DWORD XXXDlg::threadFuncTime(LPVOID threadTime)
{
CPictureprocessingDlg * pDTime = (CPictureprocessingDlg*)threadTime;
CTime CurrentTime;//時間函數
CString strTime, c_Time;
//StartTime = CTime::GetCurrentTime();
int s;
long t1 = GetTickCount();//獲取軟件運行
while (1)
{
CurrentTime = CTime::GetCurrentTime();
c_Time = CurrentTime.Format(" %Y年%m月%d日 %H:%M:%S");//轉換CString
pDTime->SetWindowText(L"界面頂部title顯示的系統時間" + c_Time);
long t2 = GetTickCount();//程序段結束後取得系統運行時間(ms)
//轉換成00:00:00形式
s = (t2 - t1) / 1000;
strTime.Format(L"%.2d:%.2d:%.2d",
s / 3600, (s - (s / 3600 * 3600)) / 60,
s - (s / 3600 * 3600) - (s - (s / 3600 * 3600)) / 60 * 60);
pDTime->SetDlgItemTextW(IDC_Time, L"軟件運行時間 " + strTime);
Sleep(1000);//休眠1000毫秒
}
return 0;
}