DuiLib(8)——Menu菜单的实现方式

一、xml写法如下




	
		
			
		
			
		
			
	


二、Menu.h

#pragma once
#include "duilib.h"

class CMenuWnd: public CXMLWnd
{
public:
    explicit CMenuWnd(LPCTSTR pszXMLName);

protected:
    virtual ~CMenuWnd();   // 私有化析构函数,这样此对象只能通过new来生成,而不能直接定义变量。就保证了delete this不会出错

public:
    void Init(CPaintManagerUI *pOwnerPM, POINT ptPos);
    virtual void    OnFinalMessage(HWND hWnd);
    virtual LRESULT HandleMessage (UINT uMsg, WPARAM wParam, LPARAM lParam);
    virtual LRESULT OnKillFocus   (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    virtual void    Notify(TNotifyUI& msg);

private:
    CPaintManagerUI *m_pOwnerPM;
};

三、Menu.cpp

#include "MenuWnd.h"

CMenuWnd::CMenuWnd( LPCTSTR pszXMLName ) 
: CXMLWnd(pszXMLName),
m_pOwnerPM(NULL)
{

}

CMenuWnd::~CMenuWnd()
{

}

void CMenuWnd::Init(CPaintManagerUI *pOwnerPM, POINT ptPos)
{
    if( pOwnerPM == NULL ) 
    {
        return;
    }

    m_pOwnerPM = pOwnerPM;
    Create(pOwnerPM->GetPaintWindow(), _T("MenuWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
    ::ClientToScreen(pOwnerPM->GetPaintWindow(), &ptPos);
    ::SetWindowPos(*this, NULL, ptPos.x, ptPos.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}

void CMenuWnd::OnFinalMessage( HWND /*hWnd*/ )
{
    delete this;
}

LRESULT CMenuWnd::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    LRESULT lRes = 0;
    BOOL bHandled = TRUE;

    switch( uMsg )
    {
    case WM_KILLFOCUS:    
        lRes = OnKillFocus(uMsg, wParam, lParam, bHandled); 
        break; 

    default:
        bHandled = FALSE;
    }

    if(bHandled || m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes)) 
    {
        return lRes;
    }

    return __super::HandleMessage(uMsg, wParam, lParam);
}

LRESULT CMenuWnd::OnKillFocus( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
    Close();
    bHandled = FALSE;
    return __super::OnKillFocus(uMsg, wParam, lParam, bHandled); 
}

void CMenuWnd::Notify( TNotifyUI& msg )
{
    if(msg.sType == DUI_MSGTYPE_ITEMCLICK)
    {
        if(m_pOwnerPM)
        {
            m_pOwnerPM->SendNotify(msg);
        }

        Close();
    }
    
    __super::Notify(msg); 
}

四、生成菜单

CMenuWnd *pMenu = new CMenuWnd(_T("menu.xml"));
POINT    pt = {msg.ptMouse.x, msg.ptMouse.y};
CDuiRect rc = msg.pSender->GetPos();

pt.x = rc.left;
pt.y = rc.bottom;
pMenu->Init(&m_PaintManager, pt);
pMenu->ShowWindow(TRUE);

五、响应

    if(msg.sType == DUI_MSGTYPE_ITEMCLICK)
    {
        CDuiString strName = msg.pSender->GetName();

        if (strName == _T("menuSequence"))
        {
            m_emPlayMode = EM_PLAY_MODE_SEQUENCE;
        } 
        else if (strName == _T("menuRandom"))
        {
            m_emPlayMode = EM_PLAY_MODE_RANDOM;
        }
        else if (strName == _T("menuSingleCircle"))
        {
            m_emPlayMode = EM_PLAY_MODE_SINGLE_CIRCLE;
        }
    }



你可能感兴趣的:(DuiLib)