Basic knowledge
The magic of MFC timers
Now there is such a problem. In VC programming, what should I do if I want to respond to an event at regular intervals?
1.Sleep (int n) function; (S must be capitalized, this is not the same as C)
2. WaitForSingleObject () wait function;
Among them, if you use Sleep () a lot, you should know that its time is not very accurate, and it will put the program to sleep, and this function is actually designed by VC for threads. The wait function requires SetEvent to achieve wakeup.
There is a third method, which is the use of a timer.
We know that in MFC of VC, we have packaged a lot of comprehensive and powerful function sets for us, so when MFC programming, cleverly calling MFC function library can save us a lot of trouble. The timer can also be used well in MFC programs.
There are three functions related to timers in MFC:
1. Set the timer (define the properties of a timer):
SetTimer (UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT * lpfnTimer) (HWND, UINT, UINT, DWORD));
2. Timer response (response to the system-defined WM_TIMER message):
OnTimer (UINT nIDEvent);
3. Release timer:
KillTimer (int nIDEvent);
among them:
UINT nIDEvent: The ID of the timer. This ID is used in a program to determine the message sent by that timer.
UINT nElapse: Defines the refresh time, that is, how often it refreshes in milliseconds.
void (CALLBACK EXPORT * lpfnTimer) (HWND, UINT, UINT, DWORD): The parameters of the callback function, the operations performed during the refresh, are generally set to 0.
Here is a simple example to illustrate how the timer is used in MFC.
1. Create a single document program Timer.
2.Define the IDs of two timers in resource.h
#define TIMER1 1
#define TIMER2 2
3. Define the properties of the two timers in the OnCreate function of CMainFrame.
SetTimer (TIMER1,3000,0);
SetTimer (TIMER2,5000,0);
4. Right-click the CMainFrame property in the class view, find WM_TIMER in the message response function, and then add the response function OnTimer ().
void CMainFrame :: OnTimer (UINT nIDEvent)
{
// TODO: Add your message handler code here and / or call default
Switch (nIDEvent)
{{
Case TIMER1:
Ruthless
AfxMessageBox ("Timer 1!");
Break;
}
Case TIMER2:
Ruthless
AfxMessageBox ("Timer 2!");
Break;
}
Default:
Break;
}
CFrameWnd :: OnTimer (nIDEvent);
}
5. Add a release timer function to the destructor of CMainFrame.
KillTimer (TIMER1);
KillTimer (TIMER2);
In this way, a simple timer program is generated. After running, you can see that a message dialog box "Timer 1" will pop up every 3 seconds, and a message dialog box "Timer 2" will pop up every 5 seconds. ".
In this case, the timer is started directly after the program is run. Many times we need to respond to an event or click a button before executing the timer, so how should we modify it? Let's also use an example to explain it, because I think that a lot of nonsense may not be worth a simple and correct example.
Now that I have created an MFC multi-document application, I want to run the timer after clicking a button on the menu bar. At this time, it is not operated in the CMainFrame, because we are going to operate the timer on the dialog box, and the CView class is responsible for the corresponding operation of the dialog box, so the timer is defined in the CView.
1. Create a multi-document program Timer.
2.Define the IDs of two timers in resource.h
#define TIMER1 1
#define TIMER2 2
3. Define the properties of the two timers in the OnInitialUpdate function of CTimerView.
SetTimer (TIMER1,3000,0);
SetTimer (TIMER2,5000,0);
4. Right-click the CTimerView property in the class view, find WM_TIMER in the message response function, and then add the response function OnTimer ().
void CTimerView :: OnTimer (UINT nIDEvent)
{
// TODO: Add your message handler code here and / or call default
// Hwnd is a variable with an initial value of false. After clicking a button, set it to true in its response function.
Switch (nIDEvent && Hwnd) {
Case TIMER1:
Ruthless
AfxMessageBox ("Timer 1!");
Break;
}
Case TIMER2:
Ruthless
AfxMessageBox ("Timer 2!");
Break;
}
Default:
Break;
}
CFrameWnd :: OnTimer (nIDEvent);
}
5. Add OnDestroy () function in CTimerView, release the timer in the function.
KillTimer (TIMER1);
KillTimer (TIMER2);
Now, after running the program, the scheduled dialog box does not pop up immediately, but the dialog box pops up after we click a button or respond to an event. Similarly, after clicking the button, the dialog box "Timer 1" "," Timer 2 "pops up every 5 seconds.
In fact, the use of timers is basically the same in different classes. As long as you find the initial function and release function of the class, you can easily implement the timer function, as simple as that.
————————————————
Copyright statement: This article is the original article of the CSDN blogger "nicolas_huan", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: https://blog.csdn.net/nicolas_huan/article/details/67632780