In the DUILIB study Note 04, you already know how to display a form, and how do you handle events and messages on a form?
I. System Messages
When the form is displayed we have already said that the form is inherited from the Cwindowwnd class, and for the processing of partial messages of the form, the Lresult Handlemessage (UINT umsg, WPARAM WPARAM, LPARAM LPARAM) of the class need to be overloaded; Function. In the Show form section we create a form wm_create message and block the title bar wm_ncactivate, wm_nccalcsize, Wm_ncpaint and other messages are handled in Handlemessage:
LRESULT cmainwnddlg::handlemessage (UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {LRESULT lres=0; BOOL bhandled=TRUE; Switch(umsg) { CaseWm_create:lres = OnCreate (umsg, WParam, LParam, bhandled); Break; CaseWm_ncactivate:lres = OnNcActivate (umsg, WParam, LParam, bhandled); Break; CaseWm_nccalcsize:lres = Onnccalcsize (umsg, WParam, LParam, bhandled); Break; CaseWm_ncpaint:lres = Onncpaint (umsg, WParam, LParam, bhandled); Break; CaseWm_nchittest:lres = Onnchittest (umsg, WParam, LParam, bhandled); Break; CaseWm_close:lres = OnClose (umsg, WParam, LParam, bhandled); Break; CaseWm_destroy:lres = OnDestroy (umsg, WParam, LParam, bhandled); Break; CaseWm_size:lres = OnSize (umsg, WParam, LParam, bhandled); Break; CaseWm_getminmaxinfo:lres = OnGetMinMaxInfo (umsg, WParam, LParam, bhandled); Break; CaseWm_syscommand:lres = OnSysCommand (umsg, WParam, LParam, bhandled); Break; CaseWm_keydown:postquitmessage (0); Break; default: bhandled=FALSE; } if(bhandled)returnLres; if(M_paintmanager.messagehandler (umsg, WParam, LParam, Lres))returnLres; returncwindowwnd::handlemessage (umsg, WParam, LParam);}
As shown in the code, if the message does not need to be processed by the framework, it is returned directly. If the framework is also required to process the message, it is referred to the parent class for processing in the Handlemessge.
two. Event Messages
For the system message we directly overloaded the handlemessage to deal with, and for the mouse click a class of messages? To do this, our forms need to inherit the Inotifyui, in addition to inheriting Cwindowwnd, the same overloaded Inotifyui class of void Notify (tnotifyui& msg); function, which is the function that handles the message generated by the control operation. But just inheriting the overloads is not enough, how can we ensure that the event messages pass properly? Therefore, when the form is created OnCreate, we also need to add the following m_paintmanager.addnotifier (this); In this way, the control message can convey a large duilib message loop, and we can also process the message through the Notify function:
voidCmainwnddlg::notify (tnotifyui&msg) { if(Msg.stype = = _t ("Windowinit") ) {onwindowinit (); } Else if(Msg.stype = = _t ("Click") ) { if(Msg.psender = =m_pclosebtn) {PostQuitMessage (0); return; } Else if(Msg.psender = =m_pminbtn) {SendMessage (Wm_syscommand, Sc_minimize,0);return; } Else if(Msg.psender = =m_pmaxbtn) {SendMessage (Wm_syscommand, Sc_maximize,0);return; } Else if(Msg.psender = =m_prestorebtn) {SendMessage (Wm_syscommand, Sc_restore,0);return; } //Button MessageOnlbtnclick (Msg.psender); } Else if(Msg.stype==_t ("selectchanged") {cduistring name= msg.psender->GetName (); Ctablayoutui* Ptabswitch = static_cast<ctablayoutui*> (M_paintmanager.findcontrol (_t ("Tab_switch"))); Ctablayoutui* Pdemolistswitch = static_cast<ctablayoutui*> (M_paintmanager.findcontrol (_t ("Demo_list_tab_switch"))); if(Name.comparenocase (_t ("Demo_tab")) ==0) Ptabswitch->selectitem (0); Else if(Name.comparenocase (_t ("Web_tab")) ==0) Ptabswitch->selectitem (1); if(Name.comparenocase (_t ("Demo_list_basic_ctrl")) ==0) Pdemolistswitch->selectitem (0); Else if(Name.comparenocase (_t ("Demo_list_rich_ctrl")) ==0) Pdemolistswitch->selectitem (1); }}
Different operations are handled for different messages in the Notify function, such as Click, Selectchanged, and so on. For this type of duilib, the message types that are customized for related operations can be viewed in the UIDefine.h file in the Duilib project:
//define all message types//////////////////////////////////////////////////////////////////////////#defineDui_msgtype_menu (_t ("menu"))#defineDui_msgtype_link (_t ("LINK"))#defineDui_msgtype_timer (_t ("TIMER"))#defineDui_msgtype_click (_t ("click"))#defineDui_msgtype_return (_t ("RETURN"))#defineDui_msgtype_scroll (_t ("SCROLL"))#defineDui_msgtype_dropdown (_t ("DROPDOWN"))#defineDui_msgtype_setfocus (_t ("SETFOCUS"))#defineDui_msgtype_killfocus (_t ("Killfocus"))#defineDui_msgtype_itemclick (_t ("ItemClick"))#defineDui_msgtype_tabselect (_t ("Tabselect"))#defineDui_msgtype_itemselect (_t ("ItemSelect"))#defineDui_msgtype_itemexpand (_t ("Itemexpand"))#defineDui_msgtype_windowinit (_t ("Windowinit"))#defineDui_msgtype_buttondown (_t ("Buttondown"))#defineDui_msgtype_mouseenter (_t ("MOUSEENTER"))#defineDui_msgtype_mouseleave (_t ("MouseLeave"))#defineDui_msgtype_textchanged (_t ("TEXTCHANGED"))#defineDui_msgtype_headerclick (_t ("Headerclick"))#defineDui_msgtype_itemdbclick (_t ("Itemdbclick"))#defineDui_msgtype_showactivex (_t ("Showactivex"))#defineDui_msgtype_itemcollapse (_t ("Itemcollapse"))#defineDui_msgtype_itemactivate (_t ("ItemActivate"))#defineDui_msgtype_valuechanged (_t ("valuechanged"))#defineDui_msgtype_selectchanged (_t ("selectchanged"))//////////////////////////////////////////////////////////////////////////
three. Message Filtering
In practice, we may sometimes need to classify some of the messages as needed. such as keyboard key messages and so on. For such cases, our form needs to inherit the Imessagefilterui class, overloading Lresult MessageHandler (UINT umsg, WPARAM WPARAM, LPARAM LPARAM, bool& bhandled ) function and add m_pm_ when the form OnCreate is created. Addpremessagefilter (this) message notification.
Duilib study Note "05"-Message response processing