VC之回调函数示例

来源:互联网 发布:临沂知豆租赁电话400 编辑:程序博客网 时间:2024/06/05 00:47

转载至:http://www.cnblogs.com/xhzxlqt/archive/2009/10/22/1588361.html

类1

定义回调函数

复制代码
class CMainFrame : public CFrameWnd
{
//
public:

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

 

回调函数的实现


void CMainFrame::NotifyProc(LPVOID lpParam, ClientContext* pContext, UINT nCode)
{
    CMainFrame
* pFrame = (CMainFrame*) lpParam;
    CIOCP_ServerView
* pView = static_cast<CIOCP_ServerView*>(pFrame->GetActiveView());
    
    
    
switch (nCode)
    {
    
//do something,here
    }
}

 

在类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);

原创粉丝点击