duilib學習筆記03:Duilib中的事件響應的兩種方式


Duilib中的事件響應有兩種方式:


 

★:在事件處理類(一般使用窗口類)中實現INotifyUI接口,然后在Notify函數中處理事件,這種方式比較簡單常用。

class CLoginFrameWnd : public CWindowWnd, public INotifyUI
{
    public:
        // ……
        void Notify(TNotifyUI& msg)
        {
            if( msg.sType == _T("click") )
            {
                if( msg.pSender->GetName() == _T("closebtn") )
                {
                    PostQuitMessage(0); return;
                }
                else if( msg.pSender->GetName() == _T("loginBtn") )
                {
                    Close();
                    return;
                }
            }
            else if( msg.sType == _T("itemselect") )
            {
                if( msg.pSender->GetName() == _T("accountcombo") )
                {
                    CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
                    if( pAccountEdit )
                    {
                        pAccountEdit->SetText(msg.pSender->GetText());
                    }
                }
            }
        }
}

 

--------------------------------------------------------------------------------

 

★:使用代理機制處理事件

class CLoginFrameWnd : public CWindowWnd, public INotifyUI
{
    public:
        // ……
        bool OnAlphaChanged(void* param) {
            TNotifyUI* pMsg = (TNotifyUI*)param;
            if( pMsg->sType == _T("valuechanged") )
            {
                m_pm.SetTransparent((static_cast<CSliderUI*>(pMsg->pSender))->GetValue());
            }

            return true;
        }

        // 需要在控件創建完成之后調用.
        void OnPrepare() 
        {
            CSliderUI* pSilder = static_cast<CSliderUI*>(m_pm.FindControl(_T("alpha_controlor")));
            if( pSilder )
            {
                pSilder->OnNotify += MakeDelegate(this, &CFrameWindowWnd::OnAlphaChanged);
            }
        }
}

 

--------------------------------------------------------------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM