为什么我在编译mfc应用程序时得到这个错误

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

本文关键字:错误 应用程序 编译 mfc 为什么      更新时间:2023-10-16

我有这样的代码:

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);
}

但是当我编译它时,我得到这个错误:

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

代码类似于Microsoft文档中的代码:

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

您应该使CTestTimeUpDlg::MyTimerProc 为静态。然而,通过这样做,您不能访问实例成员,如m_TimerDisplayValue

在这种情况下你不应该使用callback。设置lpfnTimer为NULL,作为链路样本中的第一个定时器。这样,计时器发布消息WM_TIMER,您可以通过非静态成员函数处理它。

添加:
看来这份文件(加上我上面的话)缺少解释。

执行以下操作实现WM_TIMER的处理程序。

在类声明中声明处理程序:

afx_msg void OnTimer(UINT_PTR nIDEvent);

在您的cpp文件中,添加消息映射:

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

和实现:

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

相关文章: