VC++ 显示毫秒级的时间
VC++ 显示毫秒级的时间,的时分秒后,继续显示微秒/毫秒,了解一下毫秒级的定时器,请关注最后的代码:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <sys/timeb.h>
#include <time.h>
UINT ShowTimeProc(LPVOID lParam); //定时器函数
BOOL CMilliSecondDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//开启定时器
AfxBeginThread(ShowTimeProc,this);
return TRUE;
}
void CMilliSecondDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// www.codesc.net
HCURSOR CMilliSecondDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT ShowTimeProc(LPVOID lParam)
{
CMilliSecondDlg* pDlg = (CMilliSecondDlg*)lParam;
DWORD dwStart, dwStop;
// 起始值和终止值
dwStop = GetTickCount();
while(TRUE)
{
// 上一次的终止值变成新的起始值
dwStart = dwStop;
//发送消息通知对话框该更新时间了
::SendMessage(pDlg->m_hWnd,WM_UPDATETIME,0,0);
do
{
dwStop = GetTickCount();
}while(dwStop-50<dwStart);
}
return 0;
}
//更新时间
void CMilliSecondDlg::OnUpdateTime()
{
struct _timeb timebuffer;
char *timeline;
//获得毫秒级的时间
_ftime( &timebuffer );
timeline = ctime(&(timebuffer.time));
//格式化时间
m_strTime.Format("当前时间是:%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
UpdateData(FALSE);
}