Member-function и TIMEPROC не являются совместимыми типами.
Вам нужно сделать функцию-член static
. Затем он будет работать, предполагая, что список параметров одинаков как в статической функции-члене, так и в TIMEPROC.
class CMyClass
{
public:
//modified
void (CALLBACK *TimerCbfn)(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
private:
//modified
static void CALLBACK TimeoutTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
};
Указатель на функцию, а также функция-член модифицированы. Теперь это должно работать.
Теперь, поскольку функция обратного вызова стала статической, она не может получить доступ к нестатическим членам класса, потому что у вас нет указателя this
в функции.
Чтобы получить доступ к нестатическим членам, вы можете сделать это:
class CMyClass
{
public:
static void CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
//add this static member
static std::map<UINT_PTR, CMyClass*> m_CMyClassMap; //declaration
};
//this should go in the CMyClass.cpp file
std::map<UINT_PTR, CMyClass*> CMyClass::m_CMyClassMap; //definition
static DWORD MyThreadFn( LPVOID pParam )
{
CMyClass * pMyClass = (CMyClass *)pParam;
UINT_PTR id = ::SetTimer( NULL, 0, BAS_DEFAULT_TIMEOUT, CMyClass::TimerProc);
//store the class instance with the id as key!
m_CMyClassMap[id]= pMyClass;
}
void CALLBACK CMyClass::TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
//retrieve the class instance
CMyClass *pMyClass= m_CMyClassMap[idEvent];
/*
now using pMyClass, you can access the non-static
members of the class. e.g
pMyClass->NonStaticMemberFunction();
*/
}
Я удалил TimerCbfn
из своей реализации, так как он на самом деле не нужен. Вы можете передать TimerProc
напрямую SetTimer
как последний аргумент.