duilib给UIList扩展右键菜单

duilib UIList中CListContainerElementUI的鼠标左键点击和右键点击事件,调用的是同样的方法ITEMCLICK,这样就会导致,无论点右键,还是点左键,都是选中这一个动作。
所以,改造的
第一步:修改DuiLib/Control/UIList.cpp中的方法void CListContainerElementUI::DoEvent(TEventUI& event)里的if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_RBUTTONDOWN ),需要将右键和左键拆分开,分别传递不同的事件消息(如下图)
duilib给UIList扩展右键菜单_第1张图片
原来的代码是:

if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_RBUTTONDOWN )
    {
        if( IsEnabled() ) {
            m_pManager->SendNotify(this, DUI_MSGTYPE_ITEMCLICK);
            Select();
            Invalidate();
        }
        return;
    }

修改后的代码为:

if( event.Type == UIEVENT_BUTTONDOWN)
    {
        if( IsEnabled() ) {
            m_pManager->SendNotify(this, DUI_MSGTYPE_ITEMCLICK);
            Select();
            Invalidate();
        }
        return;
    }
    if (event.Type == UIEVENT_RBUTTONDOWN) {
        if (m_pManager != NULL) {
            m_pManager->SendNotify(this, DUI_MSGTYPE_ITEMRCLICK, m_iIndex, m_iDrawIndex);//传递之后设定的消息事件
        }

        return;
    }

第二步:修改DuiLib/Core/UIDefine.h,新增一行事件声明(方便使用时调用)
duilib给UIList扩展右键菜单_第2张图片
第三步:在调用了List的窗口代码中,找到事件方法void CMainFrame::Notify(TNotifyUI& msg),并增加一下事件类型的判断

if (msg.sType == _T("itemrclick")) {
        // 获取鼠标坐标
        POINT point;
        GetCursorPos(&point);
        // 右击后点别地可以清除“右击出来的菜单”
        HWND hWnd = m_PaintManager.GetPaintWindow();
        SetForegroundWindow(hWnd);
        // 生成托盘菜单
        HMENU hPopup = CreatePopupMenu();
        AppendMenu(hPopup, MF_STRING | MF_ENABLED, 1, _T("Top"));
        AppendMenu(hPopup, MF_STRING | MF_ENABLED, 2, _T("Delete"));
        AppendMenu(hPopup, MF_SEPARATOR, NULL, NULL);
        AppendMenu(hPopup, MF_STRING | MF_ENABLED, 3, _T("Modify Name"));
        //TrackPopupMenu(hPopup, TPM_LEFTBUTTON | TPM_LEFTALIGN, point.x, point.y, 0, m_hWnd, NULL);

        int index = (int)msg.wParam;
        CDuiString strIndex;
        strIndex.Format(_T("%d"), index);

        int cmd = TrackPopupMenu(hPopup, TPM_RETURNCMD, point.x, point.y, 0, hWnd, NULL);
        switch (cmd)
        {
        case 1://Top
            
            MessageBox(hWnd, strIndex, NULL, NULL);
            break;
        case 2://Delete
            MessageBox(hWnd, L"Delete", NULL, NULL);
            break;
        case 3://Modify Name
            MessageBox(hWnd, L"Modify Name", NULL, NULL);
            break;
        default:
            break;
        }
    }

XML文件


<Window size="400,400">	
	<VerticalLayout name="body2">
		<List name="tidings_list" vscrollbar="true" header="hidden" itemselectedtextcolor="#fff3f3f3" itemselectedbkcolor="#ff00b0ff" bordersize="1">
			<ListContainerElement name="tidings_item" height="64">
				<Label text="111111111">Label>
			ListContainerElement>
			<ListContainerElement name="tidings_item" height="64">
				<Label text="111111111">Label>
			ListContainerElement>
			<ListContainerElement name="tidings_item" height="64">
				<Label text="111111111">Label>
			ListContainerElement>
		List>
	VerticalLayout>
Window>

最终效果
duilib给UIList扩展右键菜单_第3张图片

你可能感兴趣的:(DuiLib,c++)