c++ - OnTimer method not working in MFC -


i created mfc dialog based app in vs2010, , wanted add timer update picture controller every 3 seconds. ontimer method never worked.

i have used class wizard add wm_timer message queue, turned out following:

begin_message_map(cxxxxdlg, cdialogex)     on_wm_paint()     on_bn_clicked(idok, &cxxxxdlg::onbnclickedok)     on_wm_timer() end_message_map() 

in xxxxdlg.cpp, 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 declard in xxxxdlv.h:

public:     afx_msg void ontimer(uint_ptr nidevent); 

when run app, settimer returned 123. should right here. but program never reached breakpoint set in 1st line of ontimer method!

then wrote hello world project test timer. set timer in same way , worked well.

so thought onfullshow() method may problem. method used change window full screen mode. comment line , still ontimer never worked.

i have check questions here. doesn't help.

does know problem comes from? thanks!

ps. did receive warnings of memory leaks. did matter?

thanks @iinspectable. found tech support here. explains cause , tells 1 solution:

// rewrite pretranslatemessage method bool cmyapp::pretranslatemessage( msg *pmsg ) {    // if timer callback message let pass on through    // dispatchmessage call.    if( (pmsg->message==wm_timer) && (pmsg->hwnd==null) )        return false;    ...    // rest of pretranslatemessage goes here.    ...     return cwinapp::pretranslatemessage(pmsg); } 

this solution not solve problem gives me hint. pretranslatemessage method should rewritten let wm_timer pass on through dispatchmessage call. but if using pretranslatemessage deal other messages, wm_keydown example, solution above may not work. seems problem priority. in end, solve using switch instead of if:

// rewrite pretranslatemessage method bool cmyapp::pretranslatemessage( msg *pmsg ) {    // if timer callback message let pass on through    // dispatchmessage call.    switch(pmsg->message)    {     case wm_keydown: // codes     case wm_timer: return false;     ...    }    ...    // rest of pretranslatemessage goes here.    ...     return cwinapp::pretranslatemessage(pmsg); } 

i hope has similar problem.

ps. pmsg->hwnd==null removed in switch , not sure if safe.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -