How do I include bitmaps on items I've added to the end of a context menu?
I'm currently writing a Windows Explorer Shell Extension. Everything is ok so far but I'm having trouble to insert menu items WITH MenuItemBitmaps at the end of the context menu.
Here is the code I used without the bitmaps:
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!!!
}
This code does what I want. It adds a separator and two menu items at the end of the context menu when I right click in the Windows explorer.
I can also add bitmaps to these menu items with this code:
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_SUCCE开发者_JAVA技巧SS, FACILITY_NULL, 3); // 3 because we do have three menu items!!!
}
But now the menu items are placed somewhere in the middle of the context menu and not at the end. Simply setting -1
instead of uMenuIndex
doesn't work. The menu items are indeed placed at the end but the bitmaps are not shown.
Any ideas?
The documentation for SetMenuItemBitmaps
says nothing about accepting -1
as a valid position like InsertMenu
does. You know the command IDs of the items you've added, and you know they're unique, so add the bitmaps by command instead of by position.
InsertMenu(hmenu, -1, MF_STRING | MF_BYPOSITION, uidFirstCmd, _T("SimpleShlExt Test Item"));
SetMenuItemBitmaps(hmenu, uidFirstCmd, MF_BITMAP | MF_BYCOMMAND, hBitmap, hBitmap);
++uidFirstCmd;
You're ignoring the instructions that the menu host has given you regarding where to put your menu items. The only reason you've seen success so far is because the menu host hasn't added any other items after you added yours, and all the other menu extensions have played by the rules and added their items where they were told. If they decide to ignore the rules like you, then they might end up at the end instead of yours.
精彩评论