Reputation: 749
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);
}
Upvotes: 0
Views: 2101
Reputation: 1029
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;}
Upvotes: 2
Reputation: 37122
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.
Upvotes: 2