我目前正在编写一个 Windows Explorer Shell 扩展。到目前为止一切都很好,但我无法在上下文菜单的末尾插入带有 MenuItemBitmaps 的菜单项。
这是我在没有位图的情况下使用的代码:
HRESULT CSimpleShlExt::QueryContextMenu(HMENU hmenu, UINT /*uMenuIndex*/, UINT uidFirstCmd, UINT /*uidLastCmd*/, UINT uFlags)
{
InsertMenu(hmenu, -1, MF_SEPARATOR, uidFirstCmd++, _T(""));
InsertMenu(hmenu, -1, MF_STRING | MF_BYPOSITION, uidFirstCmd++, _T("SimpleShlExt Test Item"));
InsertMenu(hmenu, -1, MF_STRING | MF_BYPOSITION, uidFirstCmd++, _T("SimpleShlExt Test Item 2"));
return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 3); // 3 because we do have three menu items!!!
}
这段代码做我想要的。当我在 Windows 资源管理器中单击鼠标右键时,它会在上下文菜单的末尾添加一个分隔符和两个菜单项。
我还可以使用以下代码将位图添加到这些菜单项:
HRESULT CSimpleShlExt::QueryContextMenu(HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT /*uidLastCmd*/, UINT uFlags)
{
// load the bitmap from the resource
HBITMAP hBitmap = (HBITMAP)LoadImage((HMODULE)_AtlBaseModule.m_hInst,
MAKEINTRESOURCE(IDB_BITMAP), IMAGE_BITMAP, 16, 16, 0);
InsertMenu(hmenu, uMenuIndex++, MF_SEPARATOR, uidFirstCmd++, _T(""));
InsertMenu(hmenu, uMenuIndex++, MF_STRING | MF_BYPOSITION, uidFirstCmd++, _T("SimpleShlExt Test Item"));
SetMenuItemBitmaps(hmenu, uMenuIndex - 1, MF_BITMAP | MF_BYPOSITION, hBitmap, hBitmap);
InsertMenu(hmenu, uMenuIndex++, MF_STRING | MF_BYPOSITION, uidFirstCmd++, _T("SimpleShlExt Test Item 2"));
SetMenuItemBitmaps(hmenu, uMenuIndex - 1, MF_BITMAP | MF_BYPOSITION, hBitmap, hBitmap);
return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 3); // 3 because we do have three menu items!!!
}
但是现在菜单项被放置在上下文菜单中间的某个位置,而不是最后。简单地设置-1
而uMenuIndex
不是不起作用。菜单项确实放在最后,但未显示位图。
有任何想法吗?