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:

  1. Replace the path variable value with the path to the any folder
  2. 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