VC之回调函数示例

类1

定义回调函数

class CMainFrame : public CFrameWnd
{
//
public:

    
static void CALLBACK NotifyProc(LPVOID lpParam, ClientContext* pContext, UINT nCode);
//
}

 

回调函数的实现

Code

 

在类1中还要在适当地方,将回调函数的地址传给类2实例:

Initialize(NotifyProc) 

 

在类2 中先定义

typedef void (CALLBACK* NOTIFYPROC)(LPVOID, ClientContext*, UINT nCode);//参数类型必须与类1中回调函数类型完全一致

 

class Class2
{
//
    NOTIFYPROC    m_pNotifyProc;
//
}

 

在类2中将类1实例传来的回调函数地址指定给m_pNotifyProc

BOOL Class2::Initialize(NOTIFYPROC pNotifyProc)
{
  m_pNotifyProc = pNotifyProc;

}

 在类2中需要调用回调函数时:

m_pNotifyProc((LPVOID) m_pFrame, pContext, NC_CLIENT_CONNECT);

posted @ 2009-10-22 22:49  吾非无心  阅读(1086)  评论(0编辑  收藏  举报