?? timer3說明.txt
字號:
1:利用COleDateTime類和COleDateTimeSpan類來實現秒級延時:
在程序需要延時的地方添加如下代碼即可:
void CMulti_TimerDlg::OnButtonTime3()
{
COleDateTime start_time = COleDateTime::GetCurrentTime();
COleDateTimeSpan end_time = COleDateTime::GetCurrentTime()-start_time;
while(end_time.GetTotalSeconds() < 2) //實現延時2秒
{
end_time = COleDateTime::GetCurrentTime()-start_time;
}
}
2: 利用COleDateTime類和COleDateTimeSpan類來實現秒級定時器:
a:在頭文件中自定義定時器時間到響應消息 #define WM_UPDATETIME1 WM_USER+1000
b:在頭文件中聲明WM_UPDATETIME1 消息的響應函數OnUpdateTime1()
//{{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 OnUpdateTime1();
DECLARE_MESSAGE_MAP()
c:在*.cpp文件中映射WM_UPDATETIME1 消息,并定義響應函數,其代碼如下:
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_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)
END_MESSAGE_MAP()
void CMulti_TimerDlg::OnUpdateTime1()//即添加定時時間到的處理操作
{
struct _timeb timebuffer;
char *timeline;
//獲得毫秒級的時間
_ftime( &timebuffer );
timeline = ctime(&(timebuffer.time));
//格式化時間
m_strTime.Format("當前時間是:%.19s %s", timeline, &timeline[20]);
UpdateData(FALSE);
}
d:在*.cpp文件中定義一個線程函數,用來做定時器:
UINT ShowTime3_1_Proc(LPVOID lParam); //聲明定時器函數
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秒
{
End_time = COleDateTime::GetCurrentTime()-Start_time;
}
}
return 0;
}
e:在需要的地方開啟定時器:
void CMulti_TimerDlg::OnButtonTime31()
{
AfxBeginThread(ShowTime3_1_Proc,this);//開啟定時器
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -