I created an MFC dialog based app in VS2010, and wanted to add timer to update a picture controller every 3 seconds. But the OnTimer method never worked.
I have used Class Wizard to add WM_TIMER into the message queue, which turned out to be as following:
BEGIN_MESSAGE_MAP(CxxxxDlg, CDialogEx)
ON_WM_PAINT()
ON_BN_CLICKED(IDOK, &CxxxxDlg::OnBnClickedOK)
ON_WM_TIMER()
END_MESSAGE_MAP()
In xxxxDlg.cpp, I put SetTimer method in OnInitDialog:
BOOL CxxxxDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, TRUE);
_imageCounter = 1;
_isMale = 3;
_testNum = 0;
SetTimer(123, 2000, NULL);
bFullScreen = false;
OnFullShow();
updateImages();
UpdateData();
return TRUE;
}
The OnTimer method was declard in xxxxdlv.h:
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
When I run the app, the SetTimer returned 123. So everything should be all right here. But the program never reached the breakpoint I set in the 1st line of OnTimer method!
Then I wrote another hello world project only to test the timer. I set the timer in exactly the same way and it worked well.
So I thought the OnFullShow() method may be the problem. This method was used to change the window into full screen mode. I comment this line , but still OnTimer never worked.
I have check the questions here. But it doesn't help.
Does anyone know where the problem comes from? Thanks!
PS. I did receive some warnings of memory leaks. Did this matter?
WM_PAINT
function is somehow looping so that theWM_TIMER
message doesn't get processed? [try commenting out your paint function to check] – Edward ClementsWM_TIMER
is a low priority message (likeWM_PAINT
andWM_MOUSEMOVE
). It is only generated if the message queue is empty and there are no invalid window regions and there is no mouse input. If you fail to meet either of those conditions you will never get aWM_TIMER
, even though the timer was properly installed. – IInspectablePreTranslateMessage
method I used for the keyboard. If the level of message did matter, this method might not receiveWM_TIMER
, isn't it? I'll update my question later. Thanks again :) – thundertrick