开发者

How to modify context menu of internet explorer using IDocHostUIHandler::ShowContextMenu?

开发者 https://www.devze.com 2023-01-25 12:41 出处:网络
I am trying to implement a custom menu for Internet Explorer 7.0. For this i have to use IDocHostUIHandler::ShowContextMenu only. Till now i am able to implement a basic context menu with two options.

I am trying to implement a custom menu for Internet Explorer 7.0. For this i have to use IDocHostUIHandler::ShowContextMenu only. Till now i am able to implement a basic context menu with two options. The problem is that they are disabled by default. The sample code for the same is:

HRESULT CWebEventHandler::ShowContextMenu(DWORD dwID,POINT *ppt, IUnknown *pcmdTarget, IDispatch *pdispObject) 
{
    if (false) // I will put some guard code here. as of now do not consider it
        return S_FALSE;      // Show standard context menus.
    else
    {
        IOleWindow* pWnd = NULL;
        HRESULT hr = pcmdTarget->QueryInterface(IID_IOleWindow,
            (void**) &pWnd);

        if (SUCCEEDED(hr))
        {
            HWND hwnd;

            if (SUCCEEDED(pWnd->GetWindow(&hwnd)))
            {
                HMENU menu = ::CreatePopupMenu();
                ::AppendMenu(menu, MF_STRING, ID_HELLO, L"&Hello" ); // ID_HELLO & ID_WORLD are two menu resource items
                ::AppendMenu(menu, MF_STRING, ID_WORLD, L"&World" );

                long myRetVal = ::TrackPopupMenu(menu, 
                    TPM_RIGHTBUTTON | TPM_LEFTALIGN | TPM_RETURNCMD, 
                    ppt->x, ppt->y, NULL, hwnd, NULL);

                // Send the command to the browser.
                //
                LRESULT myResult = ::SendMessage(hwnd, WM_COMMAND,
                    myRetVal, NULL);
            }

            pWnd->Release();
        }
    }
    return S_OK; 
}

Kindly suggest what is wrong with this code & why my me开发者_如何学运维nu entries are disabled??

Thanks

EDIT

The same post is available on this link also ( http://social.msdn.microsoft.com/Forums/en/ieextensiondevelopment/thread/13584f76-21bd-4764-b5b7-e81932561574 )


I think i have solved the problem. After getting the hwnd object in if (SUCCEEDED(pWnd->GetWindow(&hwnd))) install your own CALLBACK for context menu. In the callback

if ((uMsg == WM_INITMENUPOPUP) && (wParam == (WPARAM) menu)) {
        return 0;       
    }

otherwise let the original handler process it.

once done with

long myRetVal = ::TrackPopupMenu(g_hPubMenu,TPM_RIGHTBUTTON | TPM_LEFTALIGN | TPM_RETURNCMD, 
                    ppt->x, ppt->y, NULL, hwnd, NULL);

revert back to the original proc handler....

Sample

WNDPROC g_lpPrevWndProc = NULL;

HRESULT CWebEventHandler::ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown *pcmdTarget, IDispatch *pdispObject) 
    {


if (false)
        return S_FALSE;      // Show standard context menus.
    else
    {
        IOleWindow* pWnd = NULL;
        HRESULT hr = pcmdTarget->QueryInterface(IID_IOleWindow,
            (void**) &pWnd);

        if (SUCCEEDED(hr))
        {
            HWND hwnd;

            if (SUCCEEDED(pWnd->GetWindow(&hwnd)))
            {
                g_lpPrevWndProc = (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (LONG)CtxMenuWndProc);

                if (g_hPubMenu)
                {
                    DestroyMenu(g_hPubMenu);
                    g_hPubMenu = NULL;
                }
                g_hPubMenu = ::CreatePopupMenu();

                ::AppendMenu(g_hPubMenu, MF_STRING|MF_ENABLED   , ID_HELLO, L"&Hello" );
                ::AppendMenu(g_hPubMenu, MF_STRING|MF_ENABLED   , ID_WORLD, L"&World" );

                long myRetVal = ::TrackPopupMenu(g_hPubMenu, 
                    TPM_RIGHTBUTTON | TPM_LEFTALIGN | TPM_RETURNCMD, 
                    ppt->x, ppt->y, NULL, hwnd, NULL);

                SetWindowLong(hwnd, GWL_WNDPROC, (LONG)g_lpPrevWndProc);

                // Send the command to the browser.
                //

                if (myRetVal == ID_HELLO)
                {
                    box(_T("Hello"));
                }else if(myRetVal == ID_WORLD)
                {
                    box(_T("World"));
                }else{
                        LRESULT myResult = ::SendMessage(hwnd, WM_COMMAND,myRetVal, NULL);
                }

            }

            pWnd->Release();
        }
    }
    return S_OK; 
}


LRESULT CALLBACK CtxMenuWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if ((uMsg == WM_INITMENUPOPUP) && (wParam == (WPARAM) g_hPubMenu)) {
        return 0;       
    }
    return CallWindowProc(g_lpPrevWndProc, hwnd, uMsg, wParam, lParam);
}


I'm sure you already figured this out, but in the second sample, you are doing: ::SendMessage(hwnd, WM_COMMAND,myRetVal, NULL);

after reverting: SetWindowLong(hwnd, GWL_WNDPROC, (LONG)g_lpPrevWndProc);

Your callback will never get executed on the return 0; path, though, nor was there such a case in this particular sample.

0

精彩评论

暂无评论...
验证码 换一张
取 消