0

CWinApp を拡張するクラス CDatabaseApplicationApp で ON_WM_TIMER に問題がある

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

この問題を解決する方法はありますか?

4

2 に答える 2

0

CWnd::SetTimer最後の引数として、WM_TIMER メッセージを処理するために呼び出される関数 (コールバック関数) へのポインターを取ります。

この引数が NULL に設定されている場合、そのウィンドウのメソッドが呼び出されます。つまり、クラスのメソッドOnTimerをオーバーライドする必要があります。OnTimerCDatabaseApplicationDlg

それをしたくない場合は、コールバック関数、つまりメッセージを処理するために呼び出される他の関数を明示的に指定する必要があります。これは、グローバル関数または静的クラス メンバーにすることができます。ただし、メンバー関数ポインターは実際にはポインターではないため、非静的クラスメンバーはそのままでは機能しないため、それらを別のものにラップする必要があります。

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)
于 2012-09-14T10:05:24.740 に答える
0

メインウィンドウを呼び出すSetTimerと、

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

オーバーライドは、おそらくアプリケーション クラスではなく、OnTimer(から派生した) ウィンドウ クラスにあるはずです。CWnd

于 2012-09-14T09:15:54.943 に答える