duilib List右键菜单的响应

duilib List右键菜单的响应

flyfish

版本 ultimate

自定义窗口是CCustomWnd

在头文件中定义

CListUI *m_pList;
CMenuWnd* m_pMenu;
CStdStringPtrMap m_MenuInfos;

实现文件

void CCustomWnd::InitWindow()
{
m_pList=static_cast(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文件

主要是菜单的


<Window  width="100" >
<Font ID="1234" name="微软雅黑" size="12" bold="false" default="true" />



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

  Menu>
Window>

你可能感兴趣的:(VC)