admin管理员组文章数量:1125060
When we call IContextMenu::QueryContextMenu
we pass it arguments such as idCmdFirst
and idCmdLast
. If we pass 1
as idCmdFirst and 5
as idCmdLast, then as far as I know the menu identifiers should be in the range from 1
to 5
.
However, tests show that for a text file, for example, we get more than 5
menu items whose IDs are outside the range [1, 5]
. Why does this happen?
You can see my code below. To run this code and display the menu, you need to do the following:
- Replace the
path
variable value with the path to the any folder - Press the
Show Context menu
button to display the context menu
#include <windows.h>
#include <shlobj.h>
#include <atlbase.h>
#include <strmif.h>
LPCWSTR path = L"C:\\Users\\Username\\file.txt";
IContextMenu2* _cm2 = nullptr;
IContextMenu3* _cm3 = nullptr;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (_cm3)
{
LRESULT lr = 0;
if (SUCCEEDED(_cm3->HandleMenuMsg2(message, wParam, lParam, &lr)))
return lr;
}
else if (_cm2)
{
if (SUCCEEDED(_cm2->HandleMenuMsg(message, wParam, lParam)))
return 0;
}
const int buttonId = 1;
const int buttonX = 10;
const int buttonY = 10;
switch (message)
{
case WM_CREATE:
CreateWindow(L"BUTTON", L"Show Context menu", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, buttonX, buttonY, 200, 30, hwnd, (HMENU)(long)buttonId, nullptr, nullptr);
break;
case WM_COMMAND:
if (LOWORD(wParam) == buttonId)
{
IShellItem* item;
IBindCtx* ctx;
CreateBindCtx(0, &ctx); // use a binding context to avoid the shell to be too smart and get back wrong items
SHCreateItemFromParsingName(path, ctx, IID_PPV_ARGS(&item));
ctx->Release();
if (item)
{
IContextMenu* cm;
item->BindToHandler(nullptr, BHID_SFUIObject, IID_PPV_ARGS(&cm));
if (cm)
{
if (FAILED(cm->QueryInterface(&_cm3)))
{
cm->QueryInterface(&_cm2);
}
auto menu = CreatePopupMenu();
const int firstId = 1;
const int lastId = 5;
cm->QueryContextMenu(menu, 0, firstId, lastId, CMF_NORMAL); // remove some flags if not needed
POINT pt{ buttonX, buttonY };
ClientToScreen(hwnd, &pt);
auto cmd = TrackPopupMenu(menu, TPM_RETURNCMD, pt.x, pt.y, 0, hwnd, nullptr);
if (cmd)
{
CMINVOKECOMMANDINFO cmi{};
cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
cmi.lpVerb = (LPSTR)MAKEINTRESOURCE(cmd - firstId);
cmi.nShow = SW_SHOWNORMAL;
cm->InvokeCommand(&cmi);
}
cm->Release();
DestroyMenu(menu);
if (_cm2)
{
_cm2->Release();
_cm2 = nullptr;
}
if (_cm3)
{
_cm3->Release();
_cm3 = nullptr;
}
}
item->Release();
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
auto hmod = LoadLibrary(L"shell32.dll");
auto fileIconInit = (BOOL(WINAPI*)(BOOL))GetProcAddress(hmod, MAKEINTRESOURCEA(660));
if (fileIconInit)
{
fileIconInit(TRUE);
}
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"MyWindowClass";
RegisterClass(&wc);
auto hwnd = CreateWindow(wc.lpszClassName, L"Context Menu", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
本文标签: cIContextMenuQueryContextMenu returns extra elementsStack Overflow
版权声明:本文标题:c++ - IContextMenu::QueryContextMenu returns extra elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736641783a1946012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论