程序功能:实现精度为0.01秒的计时器。在编辑框中IDC_EDIT_INPUT 输入时间,按启动按钮OnButtonStart时启动线程计时,在计时过程中实时更新静态文本控件IDC_STATIC_DISPLAY 中时间,按停止按钮OnButtonStop时静态文本控件IDC_STATIC_DISPLAY 中显示当前计时时间,当计时到达输入时间时,停止线程,同时记录下最终的时间),并重置计时器的运行状态。该程序用到计时器类CElapsed它是一个计算时间间隔的工具。

CElapsed.h相关代码
cpp
// CElapsed.h: 高精度计时器类
//
#ifndef __CELAPSED_H__
#define __CELAPSED_H__
class CElapsed
{
public:
CElapsed();
~CElapsed();
void Start(); // 开始计时
double Now(); // 获取经过的时间(毫秒)
double NowSec(); // 获取经过的时间(秒)
private:
LARGE_INTEGER m_liFreq; // 计时器频率
LARGE_INTEGER m_liStart; // 开始计时的时间点
bool m_bHighResTimer; // 是否支持高精度计时器
};
#endif // __CELAPSED_H__
CElapsed.cpp相关代码
cpp
// CElapsed.cpp: 高精度计时器类实现
//
#include "stdafx.h"
#include "CElapsed.h"
CElapsed::CElapsed()
{
// 检测是否支持高精度计时器
m_bHighResTimer = (QueryPerformanceFrequency(&m_liFreq) != 0);
m_liStart.QuadPart = 0;
}
CElapsed::~CElapsed()
{
}
// 开始计时
void CElapsed::Start()
{
if (m_bHighResTimer)
{
QueryPerformanceCounter(&m_liStart);
}
else
{
m_liStart.LowPart = GetTickCount();
m_liStart.HighPart = 0;
}
}
// 获取经过的时间(毫秒)
double CElapsed::Now()
{
if (m_bHighResTimer)
{
LARGE_INTEGER liNow;
QueryPerformanceCounter(&liNow);
return ((liNow.QuadPart - m_liStart.QuadPart) * 1000.0) / m_liFreq.QuadPart;
}
else
{
return (double)(GetTickCount() - m_liStart.LowPart);
}
}
// 获取经过的时间(秒)
double CElapsed::NowSec()
{
return Now() / 1000.0;
}
MyTimerDlg.h相关代码
cpp
// MyTimerDlg.h : header file
//
#define ID_UPDATE_DISPLAY 1001
#if !defined(AFX_MYTIMERDLG_H__055F2DFB_689D_409A_8AEB_BB38B7D1D42F__INCLUDED_)
#define AFX_MYTIMERDLG_H__055F2DFB_689D_409A_8AEB_BB38B7D1D42F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "CElapsed.h"
/////////////////////////////////////////////////////////////////////////////
// CMyTimerDlg dialog
class CMyTimerDlg : public CDialog
{
// Construction
public:
CMyTimerDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CMyTimerDlg)
enum { IDD = IDD_MYTIMER_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyTimerDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
void UpdateDisplay(double dRemainingTime);
void StopTimer(); // 停止计时器的内部函数
CElapsed m_elapsedTimer; // 高精度计时器
double m_dInputTime; // 输入的总时间(秒)
double m_dElapsedTime; // 已过去的时间(秒)
bool m_bRunning; // 计时器是否在运行
bool m_bThreadRunning; // 更新线程是否在运行
static UINT TimerThreadProc(LPVOID pParam); // 计时器线程函数
CWinThread* m_pTimerThread; // 计时器线程指针
// Implementation
protected:
HICON m_hIcon;
private:
double m_dFinalTime; // 记录停止时的最终时间
bool m_bCompleted; // 标记计时是否已完成
// Generated message map functions
//{{AFX_MSG(CMyTimerDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonStart();
afx_msg void OnButtonStop();
afx_msg LRESULT OnUpdateDisplay(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
enum { WM_UPDATE_DISPLAY = WM_USER + 100 };
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYTIMERDLG_H__055F2DFB_689D_409A_8AEB_BB38B7D1D42F__INCLUDED_)
MyTimerDlg.cpp相关代码
cpp
// MyTimerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyTimer.h"
#include "MyTimerDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyTimerDlg dialog
CMyTimerDlg::CMyTimerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyTimerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyTimerDlg)
m_bRunning = false;
m_bThreadRunning = false;
m_pTimerThread = NULL;
m_dInputTime = 0.0;
m_dElapsedTime = 0.0;
m_dFinalTime = 0.0; // 初始化最终时间
m_bCompleted = false; // 初始化完成状态
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyTimerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyTimerDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyTimerDlg, CDialog)
//{{AFX_MSG_MAP(CMyTimerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_MESSAGE(WM_UPDATE_DISPLAY, OnUpdateDisplay)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyTimerDlg message handlers
BOOL CMyTimerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetDlgItemText(IDC_STATIC_DISPLAY, _T("0.0"));
SetDlgItemText(IDC_EDIT_INPUT, _T("10.0")); // 默认10秒
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyTimerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyTimerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyTimerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyTimerDlg::OnButtonStart()
{
if (m_bRunning) return;
// 重置状态
m_bCompleted = false;
m_dFinalTime = 0.0;
// 获取编辑框中输入的时间(秒)
CString strTime;
GetDlgItemText(IDC_EDIT_INPUT, strTime);
m_dInputTime = atof(strTime);
if (m_dInputTime <= 0.0)
{
MessageBox(_T("请输入有效的时间(大于0)"), _T("错误"), MB_ICONERROR);
return;
}
// 重置计时器
m_elapsedTimer.Start();
m_bRunning = true;
m_bThreadRunning = true;
// 启动前先更新一次显示
// UpdateDisplay(m_dInputTime);
UpdateDisplay(0.0);
// 启动计时器线程
// m_bThreadRunning = true;
m_pTimerThread = AfxBeginThread(TimerThreadProc, this, THREAD_PRIORITY_NORMAL);
}
void CMyTimerDlg::OnButtonStop()
{
if (!m_bRunning) return;
if (m_bCompleted)
{
UpdateDisplay(m_dFinalTime);
return;
}
// 先获取当前时间值
// dCurrentTime = min(dCurrentTime, m_dInputTime); // 不超过输入值
// 停止计时器
StopTimer();
double dCurrentTime = m_elapsedTimer.NowSec();
// 显示停止时的当前值
UpdateDisplay(dCurrentTime);
}
void CMyTimerDlg::StopTimer()
{
if (!m_bRunning) return;
m_bThreadRunning = false;
m_bRunning = false;
if (m_pTimerThread)
{
WaitForSingleObject(m_pTimerThread->m_hThread, 1000);
m_pTimerThread = NULL;
}
}
LRESULT CMyTimerDlg::OnUpdateDisplay(WPARAM wParam, LPARAM lParam)
{
double dRemainingTime = *(double*)wParam;
UpdateDisplay(dRemainingTime);
return 0;
}
//void CMyTimerDlg::UpdateDisplay(double dRemainingTime)
void CMyTimerDlg::UpdateDisplay(double dElapsedTime)
{
CString strTime;
// double dDisplayTime = min(dElapsedTime, m_dInputTime);
strTime.Format(_T("%.2f"),dElapsedTime); // 保持0.01秒精度
SetDlgItemText(IDC_STATIC_DISPLAY, strTime);
}
UINT CMyTimerDlg::TimerThreadProc(LPVOID pParam)
{
CMyTimerDlg* pDlg = (CMyTimerDlg*)pParam;
const double UPDATE_INTERVAL = 0.01; // 0.01秒更新间隔
CElapsed updateTimer;
updateTimer.Start();
while (pDlg->m_bThreadRunning)
{
double dElapsedTime = pDlg->m_elapsedTimer.NowSec();
// 发送当前经过的时间值
double* pCurrentTime = new double(dElapsedTime);
pDlg->PostMessage(WM_UPDATE_DISPLAY, (WPARAM)pCurrentTime, 0);
// 如果达到输入时间,自动停止
if (dElapsedTime >= pDlg->m_dInputTime)
{
pDlg->m_bCompleted = true; // 标记计时完成
pDlg->m_dFinalTime = pDlg->m_dInputTime; // 记录最终时间
pDlg->m_bThreadRunning = false;
// 发送最后一次更新,确保显示正确的时间
double* pFinalTime = new double(pDlg->m_dFinalTime);
pDlg->PostMessage(WM_UPDATE_DISPLAY, (WPARAM)pFinalTime, 0);
break;
}
// 精确等待
double dWaitTime = UPDATE_INTERVAL - fmod(updateTimer.NowSec(), UPDATE_INTERVAL);
if (dWaitTime > 0)
{
Sleep(static_cast<DWORD>(dWaitTime * 1000));
}
}
pDlg->StopTimer();
return 0;
}
运行程序
