承接专栏《MFC工控项目实例二十四模拟量校正值输入》
用多媒体定时器实现0.1秒计时器
1、在SEAL_PRESSUREDlg.h文件中添加代码
#include<MMSystem.h>
#pragma comment(lib,"winmm.lib")
class CSEAL_PRESSUREDlg : public CDialog
{
public:
CSEAL_PRESSUREDlg(CWnd* pParent = NULL); // standard constructor
...
bool m_isStart,m_isStop;
UINT timerID;//自己添加的定时器ID变量
void DestroyTimer();//自己声明的销毁定时器函数
UINT CreateTimer();//自己声明的创建定时器函数
void OnTimer(UINT nIDEvent);
static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);//定时器回调函数
int t_time;
}
2 、在SEAL_PRESSUREDlg.cpp文件中添加代码
void CSEAL_PRESSUREDlg::OnTesStart()
{
if (m_isStop)
{
m_TES_START.SetIcon(IDI_TES_START);
m_TES_START.SetAlign(CButtonST::ST_ALIGN_VERT);
((CButton *)GetDlgItem(IDC_TES_START))->SetWindowText(m_isStop ? "结束测试" : "开始测试");
DestroyTimer();
m_isStop=0;
}
else
{
t_time=0;
m_TES_START.SetIcon(IDI_TES_STOP);
m_TES_START.SetAlign(CButtonST::ST_ALIGN_VERT);
((CButton *)GetDlgItem(IDC_TES_START))->SetWindowText(m_isStop ? "结束测试" : "开始测试");
CreateTimer();
m_isStop=1;
}
}
UINT CSEAL_PRESSUREDlg::CreateTimer()
{
timeBeginPeriod(1);//设置定时器设备的最小时间分辨率
timerID = timeSetEvent(100, 1, TimeProc, (DWORD)this, TIME_PERIODIC);
return timerID;//返回定时器ID
}
void CSEAL_PRESSUREDlg::DestroyTimer()
{
timeKillEvent(timerID);//销毁定时器
timeEndPeriod(1);//清除上次调用 timeBeginPeriod 函数时指定的最小计时器分辨率
}
void CALLBACK CSEAL_PRESSUREDlg::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
CSEAL_PRESSUREDlg* time_dlg = (CSEAL_PRESSUREDlg*)dwUser;
time_dlg->OnTimer(uID );
}
void CSEAL_PRESSUREDlg::OnTimer(UINT nIDEvent)
{
t_time++;
CString str_t2_6,str_t2_5,str_t2_4,str_t2_3,str_t2_2,str_t2_1;
int t2=t_time;
str_t2_5.Format("%d",t2%10);
str_t2_4.Format("%d",(t2/10)%10);
str_t2_3.Format("%d",(t2/100)%10);
str_t2_2.Format("%d",(t2/1000)%10);
str_t2_1.Format("%d",(t2/10000)%10);
SetDlgItemText(IDC_STATIC4,str_t2_5);
SetDlgItemText(IDC_STATIC3,".");
SetDlgItemText(IDC_STATIC2,str_t2_4);
SetDlgItemText(IDC_STATIC1,str_t2_3);
SetDlgItemText(IDC_STATIC0,str_t2_2);
}
运行程序