0

I have a derived CHtmlView class and I have followed the various tutorials to display my own custom context menu.

This is the complete handler:

HRESULT CChristianLifeMinistryHtmlView::CustomContextMenu(const POINT* ppt, IUnknown* pcmdtReserved)
{
    if (ppt == nullptr || pcmdtReserved == nullptr)
        return E_FAIL;

    IOleWindow* oleWnd = nullptr;
    auto hr = pcmdtReserved->QueryInterface(IID_IOleWindow, reinterpret_cast<void**>(&oleWnd));
    if (hr != S_OK && oleWnd == nullptr)
        return hr;

    HWND hwnd = nullptr;
    hr = oleWnd->GetWindow(&hwnd);
    if (hr != S_OK || hwnd == nullptr)
        return hr;

    // AJT 21.0.0 Bug fix - use the resource handle!
    auto hMainMenu = LoadMenu(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MENU_HTML_POPUP));
    if (hMainMenu == nullptr)
        return E_FAIL;

    auto hPopupMenu = GetSubMenu(hMainMenu, 0);
    if (hPopupMenu == nullptr)
    {
        ::DestroyMenu(hMainMenu);
        return E_FAIL;
    }

    if (ShowEditorSRRContextMenu())
    {
        ::RemoveMenu(hPopupMenu, IDM_VIEWSOURCE, MF_BYCOMMAND);
    }

    // #TODO add code to check if return FALSE and use GetLastError
    ::SetMenuItemBitmaps(hPopupMenu, IDM_PAGESETUP, MF_BYCOMMAND, (HBITMAP)m_bmpPageSetup, (HBITMAP)m_bmpPageSetup);
    ::SetMenuItemBitmaps(hPopupMenu, IDM_PRINTPREVIEW, MF_BYCOMMAND, (HBITMAP)m_bmpPrintPreview, (HBITMAP)m_bmpPrintPreview);
    ::SetMenuItemBitmaps(hPopupMenu, IDM_REFRESH, MF_BYCOMMAND, (HBITMAP)m_bmpRefresh, (HBITMAP)m_bmpRefresh);
    ::SetMenuItemBitmaps(hPopupMenu, IDM_FIND, MF_BYCOMMAND, (HBITMAP)m_bmpFind, (HBITMAP)m_bmpFind);  // AJT 22.0.2

    // Show shortcut menu
    const auto iSelection = ::TrackPopupMenu(hPopupMenu,
        TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
        ppt->x,
        ppt->y,
        0,
        hwnd,
        (RECT*)nullptr);

    // Send selected shortcut menu item command to shell
    if (iSelection != 0)
    {
        if (iSelection == IDM_PRINTPREVIEW)
        {
            ::SendMessage(GetParent()->GetSafeHwnd(), WM_COMMAND, ID_FILE_PRINT_PREVIEW, NULL);
        }
        else if (iSelection == CUSTOM_MENU_VIEW_XML)
        {
            ::SendMessage(GetParent()->GetSafeHwnd(), WM_COMMAND, CUSTOM_MENU_VIEW_XML, NULL);
        }
        else if (iSelection == CUSTOM_MENU_OPEN_FILE_LOCATION) // AJT 22.0.2 JIRA MSA 91
        {
            ::SendMessage(GetParent()->GetSafeHwnd(), WM_COMMAND, ID_DISPLAY_WORKING_FOLDER, NULL);
        }
        else if (iSelection == IDM_REFRESH) // AJT 21.0.6 JIRA MSA 38
        {
            ::SendMessage(GetParent()->GetSafeHwnd(), WM_COMMAND, ID_VIEW_REFRESH, NULL);
        }
        else
        {
            ::SendMessage(hwnd, WM_COMMAND, iSelection, NULL);
        }
    }

    return S_OK;
}

It gets displayed like this:

HRESULT CChristianLifeMinistryHtmlView::OnShowContextMenu(DWORD dwID, LPPOINT ppt,
    LPUNKNOWN pcmdtReserved, LPDISPATCH pdispReserved)
{
    if(ShowEditorMWBContextMenu())
        return CustomContextMenu(ppt, pcmdtReserved);

    return S_OK; // Eat it
}

Now, if I run my software (MFC project), and invoke the context menu:

enter image description here

All is good. But, if I create a temporary user account on my PC, and run my software from that temporary account:

enter image description here

Why are two of my custom menu items disabled for this user account?

  • View XML
  • Open File Location

I am using Windows 11. Is this coding issue? Is this a Windows 11 setup issue?

Update

If I right-click the app and run elevated then the context menu items are enabled. Why do I have to do that?


Background reading:

Is it not possible to add our own menu items on the CHtmlView context menu?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Presumably, there is a document underlying your `CHtmlView` - does your 'other' account have full (i.e write) access to that and it's containing folder/directory? Without such access, I guess the MFC Framework is smart enough to know that those greyed-out commands can't be executed. – Adrian Mole Mar 08 '22 at 17:20
  • @AdrianMole The editor itself creates the HTML document to display it, and it is rendered from the users application data folder. So the XML file it there, and is the file location. For example, I can still type the hotkey to go to the file location, even though the context menu is disabled. – Andrew Truckle Mar 08 '22 at 17:23
  • Hmm. Then maybe MFC is being "too clever" (it gets that from its parent operating system). What happens if you put explicit `ON_UPDATE_COMMAND_UI` handlers into your class and then enable those commands? – Adrian Mole Mar 08 '22 at 17:24
  • @AdrianMole This is a `CDialog` project, so no doc / view architecture or anything. I just don't get it. – Andrew Truckle Mar 08 '22 at 17:26
  • @AdrianMole If I run the software from the temp account with admin rights (which then need my main account pin) I notice that the two menu items are enabled. If I then do "View XML" it actually invokes admins XML file (as expected). The Open File Location does not work, even though enabled. But if I do not run elevated the context items go disabled again. – Andrew Truckle Mar 08 '22 at 18:33
  • Doesn't anyone know how the IE code underneath is building the menu and deciding to enable / disable in the context I am using here? – Andrew Truckle Mar 10 '22 at 08:22

0 Answers0