?? timer4說明.txt
字號:
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 + -