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>

原文地址:https://www.cnblogs.com/hei-hei-hei/p/13527832.html