📄 timer4˵
字号:
1:利用GetTickCount()函数来实现延时:
在程序需要延时的地方添加如下代码即可:
void CMulti_TimerDlg::OnButtonTime4()
{
SetDlgItemInt(IDC_EDIT3,0,true);
DWORD dwStart = GetTickCount();
DWORD dwEnd = dwStart;
do
{
MSG msg;
GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);
//以上四行是实现在延时或定时期间能处理其他的消息,
//虽然这样可以降低CPU的占有率,
//但降低了延时或定时精度,实际应用中可以去掉,去掉后误差降低。
dwEnd = GetTickCount()- dwStart;
} while(dwEnd <50);
SetDlgItemInt(IDC_EDIT3,(dwEnd-50),true);
MessageBox("50ms延时已到,其误差为文本框中的值!");
}
2: 利用GetTickCount()函数来实现ms级定时器:
a:在头文件中自定义定时器时间到响应消息 #define WM_UPDATETIME2 WM_USER+1001
b:在头文件中声明WM_UPDATETIME2 消息的响应函数OnUpdateTime2()
//{{AFX_MSG(CMulti_TimerDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonTime1();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnButtonTime2();
afx_msg void OnButtonTime3();
afx_msg void OnButtonTime4();
afx_msg void OnButtonTime31();
afx_msg void OnButtonTime41();
afx_msg void OnButtonTime5();
afx_msg void OnButtonTime51();
afx_msg void OnButtonTime6();
afx_msg void OnButtonTime7();
afx_msg void OnButtonTime71();
afx_msg void OnButtonTime72();
afx_msg void OnButtonTime73();
afx_msg void OnAbout();
afx_msg void OnButtonTime61();
//}}AFX_MSG
void OnUpdateTime2();
DECLARE_MESSAGE_MAP()
c:在*.cpp文件中映射WM_UPDATETIME2 消息,并定义响应函数,其代码如下:
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_UPDATETIME2,OnUpdateTime2)
END_MESSAGE_MAP()
void CMulti_TimerDlg::OnUpdateTime2()//即添加定时时间到的处理操作
{
struct _timeb timebuffer;
char *timeline;
//获得毫秒级的时间
_ftime( &timebuffer );
timeline = ctime(&(timebuffer.time));
//格式化时间
CString m_Str;
m_Str.Format("%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
SetDlgItemText(IDC_EDIT3,m_Str);
}
d:在*.cpp文件中定义一个线程函数,用来做定时器:
UINT ShowTime4_1_Proc(LPVOID lParam); //声明定时器函数
UINT ShowTime4_1_Proc(LPVOID lParam)
{
CMulti_TimerDlg* pDlg = (CMulti_TimerDlg*)lParam;
DWORD dwStart, dwStop;
dwStop = GetTickCount();// 起始值和终止值
while(TRUE)
{
dwStart = dwStop;// 上一次的终止值变成新的起始值
::SendMessage(pDlg->m_hWnd,WM_UPDATETIME2,0,0);//发送消息通知对
//话框该更新时间了
do
{
dwStop = GetTickCount();
}while(dwStop-10<dwStart);
}
return 0;
}
e:在需要的地方开启定时器:
void CMulti_TimerDlg::OnButtonTime41()
{
AfxBeginThread(ShowTime4_1_Proc,this);//开启定时器
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -