Help with MFC Timers
-
Hi guys, I tried making my own timer application, where a number increments every second. I just wonder how SetTimer() and OnTimer() works? My timer class really does not work! X| It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.:(( How does this really work? I would really appreciate it if someone can give me a simple but concrete example. Yes, there are some samples in the internet, but really, I could not understand the mechanics beneath them. Can someone provide me with simple steps on creating this simple timer class? If I could only make this work.. my timer class is super essential to make everything in my application work. To the person who could help me with this, kudos to you! :cool: Many thanks, :rose:Christina
-
Hi guys, I tried making my own timer application, where a number increments every second. I just wonder how SetTimer() and OnTimer() works? My timer class really does not work! X| It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.:(( How does this really work? I would really appreciate it if someone can give me a simple but concrete example. Yes, there are some samples in the internet, but really, I could not understand the mechanics beneath them. Can someone provide me with simple steps on creating this simple timer class? If I could only make this work.. my timer class is super essential to make everything in my application work. To the person who could help me with this, kudos to you! :cool: Many thanks, :rose:Christina
waxie wrote:
It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.
Can you show how you have initialized it? Simple example would be like this
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()//Create timer in OnCreate
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//some other stuff
SetTimer(1,1000,NULL);
return 0;
}
//handler
void CMainFrame::OnTimer(UINT nIDEvent)
{
CFrameWnd::OnTimer(nIDEvent);
//handle
}Prasad Notifier using ATL
-
Hi guys, I tried making my own timer application, where a number increments every second. I just wonder how SetTimer() and OnTimer() works? My timer class really does not work! X| It can initialize the timer but then it does not get into the OnTimer() function even when the time is due.:(( How does this really work? I would really appreciate it if someone can give me a simple but concrete example. Yes, there are some samples in the internet, but really, I could not understand the mechanics beneath them. Can someone provide me with simple steps on creating this simple timer class? If I could only make this work.. my timer class is super essential to make everything in my application work. To the person who could help me with this, kudos to you! :cool: Many thanks, :rose:Christina
For your using OnTimer() you need to inherit your class from CWnd base class. // Header file should look like this class CMyTimer: public CWnd { ... afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP() }; // Source file should look like this .... BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP() void CMyTimer::OnTimer(UINT nIDEvent) { //Todo } Do not forget to create a timer object after construction the object i.e., any where in the program you should - CMyTimer *pTimer = new CTimer; pTimer->Create(......); pTimer->SetTimer(.....); Your Timer should then work.
-
For your using OnTimer() you need to inherit your class from CWnd base class. // Header file should look like this class CMyTimer: public CWnd { ... afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP() }; // Source file should look like this .... BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP() void CMyTimer::OnTimer(UINT nIDEvent) { //Todo } Do not forget to create a timer object after construction the object i.e., any where in the program you should - CMyTimer *pTimer = new CTimer; pTimer->Create(......); pTimer->SetTimer(.....); Your Timer should then work.
-
For your using OnTimer() you need to inherit your class from CWnd base class. // Header file should look like this class CMyTimer: public CWnd { ... afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP() }; // Source file should look like this .... BEGIN_MESSAGE_MAP(CMyTimer, CWnd) ON_WM_TIMER() END_MESSAGE_MAP() void CMyTimer::OnTimer(UINT nIDEvent) { //Todo } Do not forget to create a timer object after construction the object i.e., any where in the program you should - CMyTimer *pTimer = new CTimer; pTimer->Create(......); pTimer->SetTimer(.....); Your Timer should then work.