⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multi_timerdlg.cpp

📁 VC中基于 Windows 的精确定时
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Multi_TimerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Multi_Timer.h"
#include "Multi_TimerDlg.h"

#include <sys/timeb.h>
#include <time.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
UINT wTimerRes_1ms;//定义时间间隔
UINT wAccuracy;//定义分辨率 
UINT TimerID_1ms,TimerID1_1ms;//定义定时器句柄 

UINT ShowTime3_1_Proc(LPVOID lParam);	//声明定时器函数
UINT ShowTime4_1_Proc(LPVOID lParam);	//声明定时器函数
UINT ShowTime5_1_Proc(LPVOID lParam);	//声明定时器函数
UINT ShowTime6_1_Proc(LPVOID lParam);	//声明定时器函数

void PASCAL OneMilliSecondProc(UINT wTimerID, UINT msg,DWORD dwUser,DWORD dwl,DWORD dw2); //声明回调函数
void PASCAL OneMilliSecondProc1(UINT wTimerID, UINT msg,DWORD dwUser,DWORD dwl,DWORD dw2);//声明回调函数

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMulti_TimerDlg dialog

CMulti_TimerDlg::CMulti_TimerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMulti_TimerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMulti_TimerDlg)
	timer1=1;
	Sum=0;
	m_strTime = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	wTimerRes_1ms=ONE_MILLI_SECOND;// 给时间间隔变量赋值 
	TIMECAPS tc;
	//利用函数timeGetDevCaps取出系统分辨率的取值范围,如果无错则继续 
	if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR) 	
	{ 
		// 分辨率的值不能超出系统的取值范围 
		wAccuracy=min(max(tc.wPeriodMin,TIMER_ACCURACY),tc.wPeriodMax);
		//调用timeBeginPeriod函数设置定时器的分辨率 
		timeBeginPeriod(wAccuracy); 
	} 
}

CMulti_TimerDlg::~CMulti_TimerDlg()
{
	timeKillEvent(TimerID_1ms);
	timeEndPeriod(wAccuracy);
}
void CMulti_TimerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMulti_TimerDlg)
	DDX_Text(pDX, IDC_EDIT2, m_strTime);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMulti_TimerDlg, CDialog)
	//{{AFX_MSG_MAP(CMulti_TimerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TIME1, OnButtonTime1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_TIME2, OnButtonTime2)
	ON_BN_CLICKED(IDC_BUTTON_TIME3, OnButtonTime3)
	ON_BN_CLICKED(IDC_BUTTON_TIME4, OnButtonTime4)
	ON_BN_CLICKED(IDC_BUTTON_TIME3_1, OnButtonTime31)
	ON_BN_CLICKED(IDC_BUTTON_TIME4_1, OnButtonTime41)
	ON_BN_CLICKED(IDC_BUTTON_TIME5, OnButtonTime5)
	ON_BN_CLICKED(IDC_BUTTON_TIME5_1, OnButtonTime51)
	ON_BN_CLICKED(IDC_BUTTON_TIME6, OnButtonTime6)
	ON_BN_CLICKED(IDC_BUTTON_TIME7, OnButtonTime7)
	ON_BN_CLICKED(IDC_BUTTON_TIME7_1, OnButtonTime71)
	ON_BN_CLICKED(IDC_BUTTON_TIME7_2, OnButtonTime72)
	ON_BN_CLICKED(IDC_BUTTON_TIME7_3, OnButtonTime73)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	ON_BN_CLICKED(IDC_BUTTON_TIME6_1, OnButtonTime61)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_UPDATETIME1,OnUpdateTime1)
	ON_MESSAGE(WM_UPDATETIME2,OnUpdateTime2)
	ON_MESSAGE(WM_UPDATETIME3,OnUpdateTime3)
	ON_MESSAGE(WM_UPDATETIME4,OnUpdateTime4)
	ON_MESSAGE(WM_UPDATETIME5,OnUpdateTime5)
	ON_MESSAGE(WM_UPDATETIME6,OnUpdateTime6)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMulti_TimerDlg message handlers

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

void CMulti_TimerDlg::OnButtonTime1() 
{
	int iInstallReFlag;
	iInstallReFlag=SetTimer(timer1,100,NULL);//定时100ms
	if(iInstallReFlag==0)
	{
		MessageBox("Can not install timer!");
	}
}

void CMulti_TimerDlg::OnTimer(UINT nIDEvent) 
{
	if(nIDEvent==(UINT)timer1)
	{
		Sum++;
		SetDlgItemInt(IDC_EDIT1,Sum,true);
		if(Sum==100)
		{
			KillTimer(timer1);
			MessageBox("Sum每隔100ms自加1次,一共自加100次已完成!");
			Sum=0;
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CMulti_TimerDlg::OnButtonTime2() 
{
	Sleep(200);
	MessageBox("200ms延时已到!");
}

void CMulti_TimerDlg::OnButtonTime3() 
{
	int   time_Over;
	COleDateTime      Start_time  =  COleDateTime::GetCurrentTime();  
	COleDateTimeSpan  End_time  =  COleDateTime::GetCurrentTime()-Start_time;  
	time_Over=(int)End_time.GetTotalSeconds();
	while(time_Over<3) //实现延时4秒 
	{  
		MSG  msg;  
		GetMessage(&msg,NULL,0,0);  
		TranslateMessage(&msg); 
		DispatchMessage(&msg);  
		//以上四行是实现在延时或定时期间能处理其他的消息,虽然这样可以降低CPU的占有率,
		//但降低了延时或定时精度,实际应用中可以去掉。
		End_time  =  COleDateTime::GetCurrentTime()-Start_time; 
		time_Over=(int)End_time.GetTotalSeconds();
	}//这样在延时的时候我们也能够处理其他的消息。
	SetDlgItemInt(IDC_EDIT2,time_Over,true);
	MessageBox("3s延时已到!");
}

void CMulti_TimerDlg::OnButtonTime31() 
{
	AfxBeginThread(ShowTime3_1_Proc,this);//开启定时器
}
UINT ShowTime3_1_Proc(LPVOID lParam)
{
	CMulti_TimerDlg* pDlg = (CMulti_TimerDlg*)lParam;
	while(true)
	{
		COleDateTime      Start_time  =  COleDateTime::GetCurrentTime();  
		COleDateTimeSpan  End_time  =  COleDateTime::GetCurrentTime()-Start_time;  		
		::SendMessage(pDlg->m_hWnd,WM_UPDATETIME1,0,0);
		
		while(End_time.GetTotalSeconds()<1) //实现延时1秒 
		{  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -