duilib List右鍵菜單的響應


原文:duilib List右鍵菜單的響應

flyfish

版本 ultimate

自定義窗口是CCustomWnd

在頭文件中定義

CListUI *m_pList;
CMenuWnd* m_pMenu;
CStdStringPtrMap m_MenuInfos;

實現文件

void CCustomWnd::InitWindow()
{
m_pList=static_cast<CListUI*>(m_PaintManager.FindControl(_T("main_list")));
m_pList->SetContextMenuUsed(true);
m_pList->SetItemRSelected(true);

//注意這兩個屬性
}



void CCustomWnd:::Notify(TNotifyUI& msg)
{
    if (msg.sType == DUI_MSGTYPE_MENU) //!該消息是觸發右鍵菜單
    {

        CString strName = m_pList->GetName();

        int nSel = m_pList->GetCurSel();
        CListContainerElementUI* p = (CListContainerElementUI*)(m_pList->GetItemAt(nSel));
        //可以利用p->GetTag();獲取其他信息

        CPoint point = msg.ptMouse;
        ClientToScreen(m_hWnd, &point);
        m_pMenu = CMenuWnd::CreateMenu(NULL, _T("menu.xml"), point, &m_PaintManager, &m_MenuInfos);
        CMenuUI* rootMenu = m_pMenu->GetMenuUI();

        rootMenu->RemoveAll();//移除原來的菜單,動態增加菜單
        if (rootMenu != NULL)
        {
            CMenuElementUI* pNew = new CMenuElementUI;
            pNew->SetName(_T("menu_item"));
            pNew->SetText(_T("菜單項"));
            rootMenu->Add(pNew);

        }
        m_pMenu->ResizeMenu();
    }
}

LRESULT CCustomWnd:::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    LRESULT lRes = 0;
    bHandled = FALSE;

    switch (uMsg)
    {
    case  WM_MENUCLICK:
    {
        MenuCmd* pMenuCmd = (MenuCmd*)wParam;
        if (pMenuCmd != NULL)
        {
            BOOL bChecked = pMenuCmd->bChecked;
            CDuiString sMenuName = pMenuCmd->szName;
            CDuiString sUserData = pMenuCmd->szUserData;
            CDuiString sText = pMenuCmd->szText;
            m_PaintManager.DeletePtr(pMenuCmd);

            if (sMenuName == L"menu_item") //
            {
                //處理菜單事件
            }

        }
        break;
    }
    }
}

xml文件

主要是菜單的

<?xml version="1.0" encoding="utf-8"?>
<Window  width="100" >
<Font ID="1234" name="微軟雅黑" size="12" bold="false" default="true" />

<!-- Menu樣式 -->

<Default Font="1234" name="Menu" shared="true" value="itemfont=&quot;8&quot; bordersize=&quot;1&quot; borderround=&quot;2,2&quot; bordercolor=&quot;0x303132&quot; inset=&quot;2,2,2,2&quot; itemtextpadding=&quot;30,0,0,0&quot; bkimage=&quot;file='menu/menu_bk.png' corner='26,2,2,2' source='6,6,44,24'&quot; itemhottextcolor=&quot;0xFFFF0000&quot; itemselectedbkcolor=&quot;0xFF338ACA&quot;" /> 
  <Menu>
      <MenuElement name="menu_name" text="menu_text" icon="menu/menu_pic.png" iconsize="9,9"/>

  </Menu>
</Window>

 

 
       


免責聲明!

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



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