窗口间异步事件的使用心得,中间也花了些时间,得到了群里同学的帮助,总算知道怎么用了。
- 首先在入口函数里面添加通知中心
SNotifyCenter *pNotifyCenter = new SNotifyCenter; // BLOCK: Run application { FloatWin dlgFloat; ... nRet = theApp->Run(dlgFloat.m_hWnd); } delete pNotifyCenter; delete theApp;
|
- 主窗口继承通知中心自动注册类
class FloatWin : public SHostWnd, public TAutoEventMapReg<FloatWin>//通知中心自动注册 { ... }
|
声明事件类
可以在这里面放需要传递的数据结构
class EventNotify : public TplEventArgs<EventNotify> { SOUI_CLASS_NAME(EventNotify, L"on_event_notify") public: EventNotify(SObject *pSender) :TplEventArgs<EventNotify>(pSender) {} enum { EventID = EVT_EXTERNAL_BEGIN + 30000 };
int nData; };
|
在事件接收窗口加入相关信息
//异步事件相关 bool OnEventNotify(EventArgs *e); //Event Sender ID enum { SENDER_ID = 30000 }; virtual int GetID() const { return SENDER_ID; }
//soui消息 EVENT_MAP_BEGIN() ... //<--通知中心事件 EVENT_ID_HANDLER(SENDER_ID, EventNotify::EventID, OnEventNotify) //--> ... EVENT_MAP_END()
bool FloatWin::OnEventNotify(EventArgs *e) { EventNotify *pEvt = sobj_cast<EventNotify>(e); SStringW strMsg = SStringW().Format(L"event thread, sleep = %d", pEvt->nData);
MessageBox(this->m_hWnd, L"OnEventNotify", L"ss", MB_OK); return false; }
|
- 在事件发送窗口头文件里添加
这里的事件ID应该需要和接收定义的一致。
//Event Sender ID enum { SENDER_ID = 30000 }; virtual int GetID() const { return SENDER_ID; }
|
- 产生事件
void DlgNotify::OnBtnConfirm() { EventNotify* pEvt = new EventNotify(this); // login ui pEvt->nData = 2; SNotifyCenter::getSingleton().FireEventAsync(pEvt); pEvt->Release();
OnClose(); }
|
这样就可以了。使用起来很是方便。
动态绑定事件
SWindow *pBtnClose = FindChildByID(R.id.btn_close); pBtnClose->GetEventSet()->subscribeEvent(EventCmd::EventID,Subscriber(&CMainDlg::OnCmdClose,this));
|