如何在列表控件中创建右键单击菜单?

are*_*eum 1 c++ mfc contextmenu right-click listcontrol

(IDE:Visual C++ 6.0)

我想使用列表控件来创建一个类似于 Windows 任务管理器的程序。

将通过api接收到的进程的信息(item)添加进去,通过列表控件显示出来。

我想要做的是右键单击特定项目,然后会出现一个像真正的任务管理器一样的对话框。

搜索的结果好像是使用了 OnContextMenu(CWnd*, CPoint) 函数,但是不明白是怎么工作的。

我希望你提供一个简单的例子。

谢谢 :)

Bla*_*pel 5

这是从我的一个项目中提取的工作样本。请注意,此代码适用于 VS'15,也可能适用于 VS'10。我没有 1998 年的版本……已经 20 年了。

我在代码中留下了一些注释。请仔细阅读这些内容。

CMenu m_Menu; //Class member

m_Menu.CreateMenu(); //Call this once only (I do it in PreSubclassWindow)

//class CMenuListCtrl : public CListCtrl
void CMenuListCtrl::OnContextMenu(CWnd *pWnd, CPoint ptMousePos)
{
    //Some people might use a keyboard and not the mouse
    if (ptMousePos.x == -1 && ptMousePos.y == -1)
    {
        auto nSelectedItem = GetSelectionMark(); //Get the selected item in the CListCtrl
        if (nSelectedItem == -1)
            return;

        //Find the position
        CRect itemRect;
        GetItemRect(nSelectedItem, &itemRect, LVIR_BOUNDS);
        ClientToScreen(&itemRect);
        ptMousePos.x = itemRect.left + (itemRect.Width() / 10); //Some offset to display the menu user-friendly
        ptMousePos.y = itemRect.top + itemRect.Height() / 2;
    }

    CPoint hitPoint = ptMousePos;
    ScreenToClient(&hitPoint);

    //Fix header pop-up bug
    CHeaderCtrl *pHeader = GetHeaderCtrl();
    HDHITTESTINFO hitTestHeader = {0};
    hitTestHeader.pt = hitPoint;

    //The header doesn't need a context-menu, the item does
    if (pHeader->HitTest(&hitTestHeader) != -1)
        return;

    UINT uFlags = 0;
    HitTest(hitPoint, &uFlags);
    if (uFlags & LVHT_NOWHERE)
        return;

    //Get the previously created menu
    CMenu *pPopUp = nullptr;
    pPopUp = m_Menu.GetSubMenu(0);

    if (pPopUp)
        pPopUp->TrackPopupMenu(TPM_LEFTALIGN, ptMousePos.x, ptMousePos.y, this);
}
Run Code Online (Sandbox Code Playgroud)

为了显示菜单,您必须先创建一个。

CMenu submenu;
submenu.CreatePopupMenu();
submenu.AppendMenuW(MF_STRING, IDC_COPY_POPUP,          L"&Copy");
submenu.AppendMenuW(MF_SEPARATOR);
submenu.AppendMenuW(MF_STRING, IDC_DELETE_POPUP,        L"&Delete");
m_Menu.AppendMenuW(MF_POPUP, reinterpret_cast<UINT_PTR>(submenu.m_hMenu), L"");

submenu.Detach();
Run Code Online (Sandbox Code Playgroud)