请问如何做一个计时器,并且能精确到毫秒级?

doo_fu 2002-03-06 09:10:11
请问如何做一个计时器,并且能精确到毫秒级?
MFC中的CTime类只能取当钱时间,并只能到秒。我需要从0开始计时,并且精确到毫秒的计时器。
...全文
215 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuxq 2002-03-06
  • 打赏
  • 举报
回复
timeGetTime
最好是
QueryPerformanceFrequency & QueryPerformanceCounter
cvip11 2002-03-06
  • 打赏
  • 举报
回复
settimer
kiko_lee 2002-03-06
  • 打赏
  • 举报
回复
你可以用OnTimer()就可以了,可以精确到毫秒。
sjhang 2002-03-06
  • 打赏
  • 举报
回复
// MicroDelayDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MicroSecond.h"
#include "MicroDelay.h"
#include "MicroDelayDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayDlg dialog

CMicroDelayDlg::CMicroDelayDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMicroDelayDlg::IDD, pParent)
{
puSec = (CMicroSecond*)new CMicroSecond;

//{{AFX_DATA_INIT(CMicroDelayDlg)
m_nLoopCount1 = 0;
m_nLoopCount2 = 0;
m_nLoop_per_uSecond = 0;
m_nMilliSecondPerTenSeconds = 0;
m_strError = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMicroDelayDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMicroDelayDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMicroDelayDlg, CDialog)
//{{AFX_MSG_MAP(CMicroDelayDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_DOIT, OnDoit)
ON_BN_CLICKED(IDC_INITIALIZE, OnInitialize)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayDlg message handlers

BOOL CMicroDelayDlg::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


return TRUE; // return TRUE unless you set the focus to a control
}

void CMicroDelayDlg::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 CMicroDelayDlg::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 CMicroDelayDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CMicroDelayDlg::OnDoit()
{

BeginWaitCursor();

DWORD dwStart = GetTickCount();

puSec->MicroDelay(1000000);

DWORD dwStop = GetTickCount();

EndWaitCursor();

m_nMilliSecondPerTenSeconds = dwStop - dwStart;

SetDlgItemInt( IDC_MILLISECOND_TEST, m_nMilliSecondPerTenSeconds, TRUE );

double dError = (((double)m_nMilliSecondPerTenSeconds - 1000.0) / 1000.0) * 100.0 ;
m_strError.Format("%2.1f %%", dError);

SetDlgItemText ( IDC_MILLISECOND_TEST_ERROR, m_strError);

// puSec->TweekLoopPerMicrosecond(dError);


}

void CMicroDelayDlg::OnOK()
{
delete puSec;

CDialog::OnOK();
}


void CMicroDelayDlg::Tweek()
{
int nRetry = 0;

BeginWaitCursor();

do {


DWORD dwStart = GetTickCount();

puSec->MicroDelay(1000000);

DWORD dwStop = GetTickCount();


m_nMilliSecondPerTenSeconds = dwStop - dwStart;

SetDlgItemInt( IDC_MILLISECOND_TEST, m_nMilliSecondPerTenSeconds, TRUE );

double dError = (((double)m_nMilliSecondPerTenSeconds - 1000.0) / 1000.0) * 100.0 ;

if ( dError >= 95.0 && dError <= 105.0 )
{
break;
}

m_strError.Format("%2.1f %%", dError);

SetDlgItemText ( IDC_MILLISECOND_TEST_ERROR, m_strError);

puSec->TweekLoopPerMicrosecond(dError);

nRetry ++;
} while ( nRetry < 10 );

EndWaitCursor();

}

void CMicroDelayDlg::OnInitialize()
{
Tweek();
}
sjhang 2002-03-06
  • 打赏
  • 举报
回复
// MicroDelay.h : main header file for the MICRODELAY application
//

#if !defined(AFX_MICRODELAY_H__D7406E72_9B3A_11D1_94DB_00400540824C__INCLUDED_)
#define AFX_MICRODELAY_H__D7406E72_9B3A_11D1_94DB_00400540824C__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h" // main symbols

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayApp:
// See MicroDelay.cpp for the implementation of this class
//

class CMicroDelayApp : public CWinApp
{
public:
CMicroDelayApp();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMicroDelayApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL

// Implementation

//{{AFX_MSG(CMicroDelayApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MICRODELAY_H__D7406E72_9B3A_11D1_94DB_00400540824C__INCLUDED_)
sjhang 2002-03-06
  • 打赏
  • 举报
回复
// MicroDelay.h : main header file for the MICRODELAY application
//

#if !defined(AFX_MICRODELAY_H__D7406E72_9B3A_11D1_94DB_00400540824C__INCLUDED_)
#define AFX_MICRODELAY_H__D7406E72_9B3A_11D1_94DB_00400540824C__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h" // main symbols

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayApp:
// See MicroDelay.cpp for the implementation of this class
//

class CMicroDelayApp : public CWinApp
{
public:
CMicroDelayApp();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMicroDelayApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL

// Implementation

//{{AFX_MSG(CMicroDelayApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MICRODELAY_H__D7406E72_9B3A_11D1_94DB_00400540824C__INCLUDED_)
cqa 2002-03-06
  • 打赏
  • 举报
回复
在子线程里用::GetTickCount().
sjhang 2002-03-06
  • 打赏
  • 举报
回复
// MicroDelay.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MicroDelay.h"
#include "MicroDelayDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayApp

BEGIN_MESSAGE_MAP(CMicroDelayApp, CWinApp)
//{{AFX_MSG_MAP(CMicroDelayApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayApp construction

CMicroDelayApp::CMicroDelayApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMicroDelayApp object

CMicroDelayApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMicroDelayApp initialization

BOOL CMicroDelayApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CMicroDelayDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧