如何显示Windows资源管理器上下文(右键)菜单?

5

我希望展示Windows资源管理器的上下文菜单。

我不想把我的应用程序添加到其中,我只想在我的应用程序内显示它。

Total Commander是我需要的实现的一个很好的例子。

如果按住右键,TC将显示上下文菜单,这与Windows资源管理器中的完全相同。

我使用C++/Qt,但这里语言并不重要。

4个回答

11

我找到了几个样例,可能会对你有所帮助。由于上下文菜单高度依赖于操作系统,因此仅使用Qt可能无法完成此操作;可能还需要一些Win32调用。

Raymond Chen的博客系列“如何托管IContextMenu”

还有一些非C++样例:

还有相关的SO问题:


0

这是我的做法:

bool CShellMenu::openShellContextMenuForObject(const std::wstring &path, int xPos, int yPos, void * parentWindow)
{
    assert (parentWindow);
    ITEMIDLIST * id = 0;
    std::wstring windowsPath = path;
    std::replace(windowsPath.begin(), windowsPath.end(), '/', '\\');
    HRESULT result = SHParseDisplayName(windowsPath.c_str(), 0, &id, 0, 0);
    if (!SUCCEEDED(result) || !id)
        return false;
    CItemIdListReleaser idReleaser (id);

    IShellFolder * ifolder = 0;

    LPCITEMIDLIST idChild = 0;
    result = SHBindToParent(id, IID_IShellFolder, (void**)&ifolder, &idChild);
    if (!SUCCEEDED(result) || !ifolder)
        return false;
    CComInterfaceReleaser ifolderReleaser (ifolder);

    IContextMenu * imenu = 0;
    result = ifolder->GetUIObjectOf((HWND)parentWindow, 1, (const ITEMIDLIST **)&idChild, IID_IContextMenu, 0, (void**)&imenu);
    if (!SUCCEEDED(result) || !ifolder)
        return false;
    CComInterfaceReleaser menuReleaser(imenu);

    HMENU hMenu = CreatePopupMenu();
    if (!hMenu)
        return false;
    if (SUCCEEDED(imenu->QueryContextMenu(hMenu, 0, 1, 0x7FFF, CMF_NORMAL)))
    {
        int iCmd = TrackPopupMenuEx(hMenu, TPM_RETURNCMD, xPos, yPos, (HWND)parentWindow, NULL);
        if (iCmd > 0)
        {
            CMINVOKECOMMANDINFOEX info = { 0 };
            info.cbSize = sizeof(info);
            info.fMask = CMIC_MASK_UNICODE;
            info.hwnd = (HWND)parentWindow;
            info.lpVerb  = MAKEINTRESOURCEA(iCmd - 1);
            info.lpVerbW = MAKEINTRESOURCEW(iCmd - 1);
            info.nShow = SW_SHOWNORMAL;
            imenu->InvokeCommand((LPCMINVOKECOMMANDINFO)&info);
        }
    }
    DestroyMenu(hMenu);

    return true;
}

0
你有两个选择:
1)自己实现每个功能,在自定义上下文菜单中创建相应的操作,或者
2)访问Windows API...这正是Qt不打算考虑的,因为Qt是跨平台的。

1
这就是为什么有人发明了#ifdef的原因之一 ;) - Mariusz Jaskółka

-1

http://www.ffuts.org/blog/right-click-context-menus-with-qt/

在Qt中,使右键单击弹出上下文菜单非常简单。只需要注意一些细节即可...
 // myWidget is any QWidget-derived class
 myWidget->setContextMenuPolicy(Qt::CustomContextMenu);
 connect(myWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
     this, SLOT(ShowContextMenu(const QPoint&)));

如果你正在寻找类似于“Windows资源管理器集成”或“Windows Shell集成”的东西,那么这里有一个很好的(虽然不是QT特定的)例子:

http://www.codeproject.com/Articles/15171/Simple-shell-context-menu

关键是实现这两个Windows shell接口:

  • IContextMenu

  • IShellExtInt


谢谢回复,但我知道如何显示上下文菜单 :) 你提到的文章解释了如何将项目添加到资源管理器上下文菜单中。再次强调,我只需要显示它(默认项为“打开”,“打印”,“编辑”,“使用...”等)。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接