Duilib类似MFC的消息响应如下:
DUI_DECLARE_MESSAGE_MAP() DUI_BEGIN_MESSAGE_MAP(CYmChatUI, CXMLWnd) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick) DUI_END_MESSAGE_MAP()
绑定消息响应函数:DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick)
其他的消息类型如下,用法雷同:
#define DUI_MSGTYPE_MENU (_T("menu")) #define DUI_MSGTYPE_LINK (_T("link")) #define DUI_MSGTYPE_TIMER (_T("timer")) #define DUI_MSGTYPE_CLICK (_T("click")) #define DUI_MSGTYPE_RETURN (_T("return")) #define DUI_MSGTYPE_SCROLL (_T("scroll")) #define DUI_MSGTYPE_DROPDOWN (_T("dropdown")) #define DUI_MSGTYPE_SETFOCUS (_T("setfocus")) #define DUI_MSGTYPE_KILLFOCUS (_T("killfocus")) #define DUI_MSGTYPE_ITEMCLICK (_T("itemclick")) #define DUI_MSGTYPE_TABSELECT (_T("tabselect")) #define DUI_MSGTYPE_ITEMSELECT (_T("itemselect")) #define DUI_MSGTYPE_ITEMEXPAND (_T("itemexpand")) #define DUI_MSGTYPE_WINDOWINIT (_T("windowinit")) #define DUI_MSGTYPE_BUTTONDOWN (_T("buttondown")) #define DUI_MSGTYPE_MOUSEENTER (_T("mouseenter")) #define DUI_MSGTYPE_MOUSELEAVE (_T("mouseleave")) #define DUI_MSGTYPE_TEXTCHANGED (_T("textchanged")) #define DUI_MSGTYPE_HEADERCLICK (_T("headerclick")) #define DUI_MSGTYPE_ITEMDBCLICK (_T("itemdbclick")) #define DUI_MSGTYPE_SHOWACTIVEX (_T("showactivex")) #define DUI_MSGTYPE_ITEMCOLLAPSE (_T("itemcollapse")) #define DUI_MSGTYPE_ITEMACTIVATE (_T("itemactivate")) #define DUI_MSGTYPE_VALUECHANGED (_T("valuechanged")) #define DUI_MSGTYPE_SELECTCHANGED (_T("selectchanged"))
也可以具体到是什么事件,什么控件,映射到某个函数
//定义消息类型--执行函数宏 #define DUI_ON_MSGTYPE(msgtype, memberFxn) \ { msgtype, _T(""), DuiSig_vn, (DUI_PMSG)&memberFxn}, \ //定义消息类型--控件名称--执行函数宏 #define DUI_ON_MSGTYPE_CTRNAME(msgtype,ctrname,memberFxn) \ { msgtype, ctrname, DuiSig_vn, (DUI_PMSG)&memberFxn }, \ //定义click消息的控件名称--执行函数宏 #define DUI_ON_CLICK_CTRNAME(ctrname,memberFxn) \ { DUI_MSGTYPE_CLICK, ctrname, DuiSig_vn, (DUI_PMSG)&memberFxn }, \ //定义selectchanged消息的控件名称--执行函数宏 #define DUI_ON_SELECTCHANGED_CTRNAME(ctrname,memberFxn) \ { DUI_MSGTYPE_SELECTCHANGED,ctrname,DuiSig_vn,(DUI_PMSG)&memberFxn }, \ //定义killfocus消息的控件名称--执行函数宏 #define DUI_ON_KILLFOCUS_CTRNAME(ctrname,memberFxn) \ { DUI_MSGTYPE_KILLFOCUS,ctrname,DuiSig_vn,(DUI_PMSG)&memberFxn }, \ //定义menu消息的控件名称--执行函数宏 #define DUI_ON_MENU_CTRNAME(ctrname,memberFxn) \ { DUI_MSGTYPE_MENU,ctrname,DuiSig_vn,(DUI_PMSG)&memberFxn }, \ //定义与控件名称无关的消息宏 //定义timer消息--执行函数宏 #define DUI_ON_TIMER() \ { DUI_MSGTYPE_TIMER, _T(""), DuiSig_vn,(DUI_PMSG)&OnTimer }, \
virtual void Notify(TNotifyUI& msg);举例如下:
void CYmChatUI::Notify(TNotifyUI& msg) { if (msg.sType == _T("click")) { if (_tcsicmp(msg.pSender->GetName(), _T("signaturetip")) == 0) { AfxMessageBox(L"点击了name为signaturetip的控件"); } } __super::Notify(msg); }
比如,最大化,最小化等按钮,只要这些按钮的名字与类中定义的一样即可
(minbtn,maxbtn,restorebtn,closebtn)
而且,WindowImplBase也实现了一些常规消息的响应函数,我们只要继承即可
如下:
//点击事件 virtual void OnClick(TNotifyUI& msg); //关闭窗口事件 virtual LRESULT OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //销毁窗口事件 virtual LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //窗口尺寸改变时响应此事件 virtual LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); //拦截字符响应 virtual LRESULT OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); //拦截系统消息响应 virtual LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); //窗口创建时响应 virtual LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); //键盘按下时响应 virtual LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //失去焦点时响应 virtual LRESULT OnKillFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //获得焦点时响应 virtual LRESULT OnSetFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //左键按下响应 virtual LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //左键抬起响应 virtual LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //鼠标移动响应 virtual LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //鼠标滚轮响应 virtual LRESULT OnMouseWheel(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled); //鼠标放在控件上响应 virtual LRESULT OnMouseHover(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
void CYmChatUI::OnClick(TNotifyUI& msg) { // 获取产生消息的控件名称 CDuiString msgName = msg.pSender->GetName(); if (_tcsicmp(msgName, _T("friendbtn")) == 0) { AfxMessageBox(L"点击了朋友"); } else if (_tcsicmp(msgName, _T("groupbtn")) == 0) { AfxMessageBox(L"点击了群组"); } __super::OnClick(msg); }
LRESULT CYmChatUI::OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"您按了键盘上的%c键", wParam); AfxMessageBox(strTip); return __super::OnChar(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { AfxMessageBox(L"窗口即将关闭"); return __super::OnClose(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { AfxMessageBox(L"窗口即将销毁");//发生在OnClose之后 return __super::OnDestroy(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { AfxMessageBox(L"窗口即将创建,马上显示");//发生在窗口显示出来之前的一霎那 return __super::OnCreate(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"您按了键盘上的%c键", LOWORD(wParam));//如果按了ESC,窗口会退出异常,不知道为什么 AfxMessageBox(strTip); return __super::OnChar(uMsg, wParam, lParam, bHandled); }七、OnSize
LRESULT CYmChatUI::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip = _T("窗口大小改变了"); AfxMessageBox(strTip); return __super::OnSize(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"鼠标位置x=%d,y=%d",HIWORD(lParam), LOWORD(lParam)); AfxMessageBox(strTip); return __super::OnLButtonDown(uMsg, wParam, lParam, bHandled); }九、OnLButtonUp
LRESULT CYmChatUI::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"鼠标位置x=%d,y=%d", HIWORD(lParam), LOWORD(lParam)); AfxMessageBox(strTip); return __super::OnLButtonDown(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"鼠标位置x=%d,y=%d", HIWORD(lParam), LOWORD(lParam)); AfxMessageBox(strTip); return __super::OnMouseMove(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnMouseWheel(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"鼠标滚动位置x=%d,y=%d", HIWORD(lParam), LOWORD(lParam)); AfxMessageBox(strTip); return __super::OnMouseWheel(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnMouseHover(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { CString strTip; strTip.Format(L"鼠标停留的位置x=%d,y=%d", HIWORD(lParam), LOWORD(lParam)); AfxMessageBox(strTip); return __super::OnMouseHover(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { AfxMessageBox(L"触发了系统消息"); return __super::OnSysCommand(uMsg, wParam, lParam, bHandled); }
LRESULT CYmChatUI::ResponseDefaultKeyEvent(WPARAM wParam) { CString strTip; strTip.Format(L"点击了冷门按键%d,如ESC,ENTER", wParam); AfxMessageBox(strTip); return __super::ResponseDefaultKeyEvent(wParam); }