繁体   English   中英

如何将多个菜单项添加到 Windows Shell 扩展?

[英]Howto add multiple menu items to windows shell extension?

我正在尝试将多个菜单项添加到 windows shell 的上下文菜单中。 到目前为止,我所做的是以下代码,它将我的项目添加为子菜单,而我希望它们位于主上下文菜单上。 这是一张照片:我拥有的与我想要的

有任何想法吗? 谢谢!

STDMETHODIMP CFileFavShellExt::QueryContextMenu (HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
    // This is not our business.
    if (CMF_DEFAULTONLY & uFlags)
    {
        return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
    }

    UINT uID = uidFirstCmd;

    if (!InsertMenu(hmenu, uMenuIndex, MF_SEPARATOR | MF_BYPOSITION, 0, NULL))
    {
        return HRESULT_FROM_WIN32(GetLastError());
    }

    // Creating my menu.
    HMENU hSubmenu = CreateMenu();

    InsertMenu (hSubmenu, 0, MF_BYPOSITION, uID++, _T("Add to bookmarks 1"));
    InsertMenu (hSubmenu, 1, MF_BYPOSITION, uID++, _T("Add to bookmarks 2"));

    MENUITEMINFO mii = { sizeof(mii) };
    mii.fMask = MIIM_SUBMENU | MIIM_ID | MIIM_STRING;
    mii.hSubMenu = hSubmenu;
    mii.fType = MFT_STRING;
    mii.dwTypeData = _T("Bla");
    mii.wID = uID++;

    if (!InsertMenuItem(hmenu, uMenuIndex, TRUE, &mii))
    {
        return HRESULT_FROM_WIN32(GetLastError());
    }

    if (!InsertMenu(hmenu, uMenuIndex, MF_SEPARATOR | MF_BYPOSITION, 0, NULL))
    {
        return HRESULT_FROM_WIN32(GetLastError());
    }

    return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd);
}

编辑:我在不使用子菜单的情况下尝试了另一种方法,但仍然没有运气,我得到的只是“菜单 1”而“菜单 2”丢失了......

在此处输入图片说明

STDMETHODIMP CFileFavShellExt::QueryContextMenu (HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
    // This is not our business.
    if (CMF_DEFAULTONLY & uFlags)
    {
        return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
    }

    UINT uID = uidFirstCmd;
    UINT pos = uMenuIndex;

    MENUITEMINFO mii = { sizeof(mii) };
    mii.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE;
    mii.fType = MFT_STRING;
    mii.dwTypeData = _T("Menu 1");
    mii.fState = MFS_ENABLED;
    mii.wID = uID++;

    if (!InsertMenuItem(hmenu, pos++, TRUE, &mii))
    {
        return HRESULT_FROM_WIN32(GetLastError());
    }

    MENUITEMINFO mii2 = { sizeof(mii) };
    mii2.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE;
    mii2.fType = MFT_STRING;
    mii2.dwTypeData = _T("Menu 2");
    mii2.fState = MFS_ENABLED;
    mii2.wID = uID++;

    if (!InsertMenuItem(hmenu, pos++, TRUE, &mii2))
    {
        return HRESULT_FROM_WIN32(GetLastError());
    }

    return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd);
}

子菜单在那里是因为您正在添加它。

MENUITEMINFO mii = { sizeof(mii) };
mii.fMask = MIIM_SUBMENU | MIIM_ID | MIIM_STRING;
mii.hSubMenu = hSubmenu;
mii.fType = MFT_STRING;
mii.dwTypeData = _T("Bla");
mii.wID = uID++;

if (!InsertMenuItem(hmenu, uMenuIndex, TRUE, &mii))
{
    return HRESULT_FROM_WIN32(GetLastError());
}

这将添加“Bla”子菜单,您已向其中添加了其他项目。 要消除子菜单,只需消除此代码,然后将其他项目直接添加到主菜单。

虽然这个问题是很久以前发布的,但也许我发现了问题。 我浪费了大约两天的时间来解决同样的问题。 我的问题是在GetCommandString函数中,我为所有命令返回了相同的字符串,所以我认为 shell 管理器假定所有命令都是相同的,并且只添加了一个命令。 例如,解决方案是为不同的命令返回不同的字符串

IFACEMETHODIMP CLASSNAME::GetCommandString(UINT_PTR command_id, UINT flags, UINT * reserved, LPSTR name, UINT size)
{

    HRESULT hr = S_FALSE;
    if( command_id == 1)
        hr = StringCchCopy(reinterpret_cast<PWSTR>(name), size,
            L"some help text");
    if(command_id ==0)
        hr = StringCchCopy(reinterpret_cast<PWSTR>(name), size,
            L"some other help text");
    return hr;}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM