静态强制转换的MFC ON_WM_TIMER()问题

时间:2012-09-14 09:01:19

标签: c++ mfc

在我的类CDatabaseApplicationApp中遇到ON_WM_TIMER问题,该问题扩展了CWinApp

> 1>c:\programs\databaseapplication\databaseapplication\databaseapplication.cpp(20):
> error C2440: 'static_cast' : cannot convert from 'void (__thiscall
> CDatabaseApplicationApp::* )(UINT_PTR)' to 'void (__thiscall CWnd::*
> )(UINT_PTR)' 1>          Types pointed to are unrelated; conversion
> requires reinterpret_cast, C-style cast or function-style cast

我已经加入了OnTimer函数:

class CLifescanDatabaseApplicationApp : public CWinApp
{
public:
    CLifescanDatabaseApplicationApp();
protected:
    CLifescanDatabaseApplicationDlg * m_ptheWindow;
// Overrides
public:
    virtual BOOL InitInstance();

// Implementation
    afx_msg void OnTimer(UINT_PTR nTimerID);
    DECLARE_MESSAGE_MAP()
};

OnTimer只是:

void CDatabaseApplicationApp::OnTimer(UINT_PTR nTimerID)
{
    AfxMessageBox(_T("Help"));
}

使用源文件顶部的define设置计时器:

#define ID_TIMER_DATABASEQUERY 1

SetTimer

中定义
BOOL CDatabaseApplicationApp::InitInstance()
{
    // InitCommonControlsEx() is required on Windows XP if an application
    // manifest specifies use of ComCtl32.dll version 6 or later to enable
    // visual styles.  Otherwise, any window creation will fail.
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // Set this to include all the common control classes you want to use
    // in your application.
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();


    AfxEnableControlContainer();
AfxInitRichEdit2();
    // Create the shell manager, in case the dialog contains
    // any shell tree view or shell list view controls.
    CShellManager *pShellManager = new CShellManager;

    // Standard initialization
    // If you are not using these features and wish to reduce the size
    // of your final executable, you should remove from the following
    // the specific initialization routines you do not need
    // Change the registry key under which our settings are stored
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));



    m_ptheWindow = new CDatabaseApplicationDlg();
    m_pMainWnd = m_ptheWindow;
    if(m_ptheWindow!=nullptr)
    {
        m_ptheWindow->Create(CDatabaseApplicationDlg::IDD,CWnd::GetDesktopWindow());
        m_ptheWindow->ShowWindow(SW_SHOW);
    }
    // Delete the shell manager created above.
    if (pShellManager != NULL)
    {
        delete pShellManager;
    }

    if(!m_ptheWindow->SetTimer(ID_TIMER_DATABASEQUERY,10000,nullptr))
    {
        return false;
    }
    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return TRUE;
}

我有什么想法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

如果您为主窗口调用SetTimer

if(!m_ptheWindow->SetTimer(ID_TIMER_DATABASEQUERY,10000,nullptr))
 {
     return false;
 } 

OnTimer覆盖应该在窗口类中(从CWnd派生),而不是在应用程序类中。

答案 1 :(得分:0)

CWnd::SetTimer将指向函数的指针作为最后一个参数,该函数将被调用以处理WM_TIMER消息(回调函数)。

如果此参数设置为NULL,则将调用该窗口的OnTimer方法,这意味着您必须覆盖OnTimer类的CDatabaseApplicationDlg方法。

如果您不想这样做,则需要显式指定回调函数,即将调用其他函数来处理消息。这可以是全局函数或静态类成员。但是,非静态类成员不会开箱即用,因为成员函数指针不是真正的指针,所以你需要将它们包装成其他东西。

如果您的CDatabaseApplication课程中有静态成员,请执行以下操作:

void CDatabaseApplicationApp::OnTimer(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
   // in your case:
   // hWnd => HWND of your ID_TIMER_DATABASEQUERY instance
   // nMsg => WM_TIMER
   // nIDEvent => ID_TIMER_DATABASEQUERY, unless you also set other timers
   // dwTime => elapsed time, same as value of GetTickCount()
   AfxMessageBox(_T("Help"));
}

然后您可以像这样设置计时器:

m_ptheWindow->SetTimer(ID_TIMER_DATABASEQUERY, 10000, CDatabaseApplicationApp::OnTimer)