mans
mans

Reputation: 18168

why am I getting this error when compiling an mfc application?

I have this code:

void CALLBACK CTestTimeUpDlg::MyTimerProc(
   HWND hWnd,      // handle of CWnd that called SetTimer
   UINT nMsg,      // WM_TIMER
   UINT_PTR nIDEvent,   // timer identification
   DWORD dwTime    // system time
)
{
    const int m_TimerValue=0;
    double timeValueSec=m_TimerValue/1000.0;
    CString valueString;
    valueString.Format(L"%3.3f",timeValueSec);
    m_TimerDisplayValue.SetWindowTextW(valueString);
}


void CTestTimeUpDlg::OnBnClickedButtonStart()
{
    m_TimerValue=0;
    m_Timer = SetTimer(1, 1, &CTestTimeUpDlg::MyTimerProc);

}

but when I compiled it, I am getting this error:

 'CWnd::SetTimer' : cannot convert parameter 3 from 'void (__stdcall CTestTimeUpDlg::* )(HWND,UINT,UINT_PTR,DWORD)' to 'void (__stdcall *)(HWND,UINT,UINT_PTR,DWORD)'   

the code is similar to the code from Microsoft documentation:

http://msdn.microsoft.com/en-us/library/49313fdf.aspx

Upvotes: 0

Views: 508

Answers (1)

Ripple
Ripple

Reputation: 1265

You should make CTestTimeUpDlg::MyTimerProc static. However, by doing this, you can't access instance members such as m_TimerDisplayValue.

You shouldn't use callback in this case. Set lpfnTimer NULL, as the first timer in the sample of the link. That way, the timer posts the message WM_TIMER, and you can handle it by your non-static member function.

ADD:
Seems the document (plus my words above) is lacking in explanation.

Do as followings to implement a handler of WM_TIMER.

Declare handler in your class declaration:

afx_msg void OnTimer(UINT_PTR nIDEvent);

In your cpp file, add message mapping:

BEGIN_MESSAGE_MAP(CTestTimeUpDlg, ...)
    ON_WM_TIMER()
END_MESSAGE_MAP()

and implementation:

void CTestTimeUpDlg::OnTimer(UINT_PTR nIDEvent)
{
    // your code here...
}

Upvotes: 2

Related Questions