简体   繁体   中英

Howto add multiple menu items to windows shell extension?

I am trying to add multiple menu items to the context menu of the windows shell. What I have done so far is the following code, this adds my items as sub-menus while I want them to be on the main context menu. Here is a pic:我拥有的与我想要的

Any ideas? thx!

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);
}

EDIT: I tried another method without using a sub menu and still no luck, all I get is "Menu 1" while "Menu 2" is missing...

在此处输入图片说明

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);
}

The submenu is there because you're adding it.

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());
}

This adds the "Bla" submenu, to which you have added your other items. To eliminate the submenu simply eliminate this code, and add your other items directly to the main menu.

although this question was posted long time ago,maybe i found the problem. I was wasted about two days to same problem. My poroblem was that in GetCommandString function I've returned the same string for all my commands, so I thing shell manager assumes all command as the same and was adding only one command. the solution was to return different string for different commands for example

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;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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