Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Control the Context Menu in an ATL HTML Control


View products that this article applies to.

This article was previously published under Q274202

↑ Back to the top


Summary

This article describes how to control the context menu behavior in a Microsoft Active Template Library (ATL) HTML control.

↑ Back to the top


More information

When hosting an ATL HTML control, ATL creates a host window and uses the IAxWinAmbientDispatch interface to allow you to override ATL's default implementation of the IDocHostUIHandler interface. IAxWinAmbientDispatch exposes properties that you can use to control the host behavior, such as allowing context menus, setting default host flags, and so on.

There are two ways to control the context menu behavior in an ATL HTML control:
  • Cancel the default context menu.
  • Display a custom context menu.

Cancel the Default Context Menu

To turn off context menus, perform the following steps:
  1. Use the IAxWinAmbientDispatch::put_AllowContextMenu method to set the m_bAllowContextMenu property to true.
  2. In the control's OnCreate method that the ATL Wizard generated, add the following code:
    LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
    	CAxWindow wnd(m_hWnd);
    	HRESULT hr = wnd.CreateControl(IDH_BRHTMLCTRL);
    	if (SUCCEEDED(hr))
    		hr = wnd.SetExternalDispatch(static_cast<IBRHtmlCtrlUI*>(this));
    CComPtr<IUnknown> spUnk;					//**ADDED**
    AtlAxGetHost(m_hWnd, &spUnk);				//**ADDED**
    CComQIPtr<IAxWinAmbientDispatch> spWinAmb(spUnk);		//**ADDED**
    spWinAmb->put_AllowContextMenu(VARIANT_FALSE);		//**ADDED**
    		if (SUCCEEDED(hr))
    			hr = wnd.QueryControl(IID_IWebBrowser2, (void**)&m_spBrowser);
    		return SUCCEEDED(hr) ? 0 : -1;
    	}
    					

Display a Custom Context Menu

If you want to display your own custom context menu, you must use the SetExternalUIHandler function of the CAxWindow class to override the default implementation of IDocHostUIHandler.

This method accepts a IDocHostUIHandlerDispatch pointer instead of a IDocHostUIHandler pointer. The IDocHostUIHandlerDispatch interface is defined in the ATL header file (Atliface.h) and has the exact same methods. The parameters of some of these methods differ slightly from IDocHostUIHandler in that they use Automation-compatible types. To implement IDocHostUIHandlerDispatch, perform the following steps:
  1. In the derivation list for your control class, add IDocHostUIHandlerDispatch as follows:

    NOTE: Before you add this line of code, make sure that you type a comma at the end of your derivation list.
    public IDispatchImpl<IDocHostUIHandlerDispatch, &IID_IDocHostUIHandlerDispatch, &LIBID_ATLLib>
    						
    Instead of implementing all of the IDispatch methods, we can use the IDispatchImpl class.
  2. Add your interface to the COM interface map as follows:
    COM_INTERFACE_ENTRY(IDocHostUIHandlerDispatch)
    					
  3. Implement all of the methods for IDocHostUIHandlerDispatch. You can copy the function headers from Atliface.h and safely return E_NOTIMPL for methods that you are not interested in.
  4. In the OnCreate method that the ATL Wizard generated, set the external handler as follows:
    hr = wnd.SetExternalUIHandler(static_cast<IDocHostUIHandlerDispatch*>(this));
    					
  5. To cancel the context menu, add the following code in your IDocHostUIHandlerDispatch::ShowContextMenu implementation:
    *dwRetVal = S_OK;      //This is what the WebBrowser control is looking for.
     //You can show your own context menu here.
     return S_OK;                     
    					

↑ Back to the top


References

For additional information on how to control the WebBrowser control when you use an ATL HTML control, click the article number below to view the article in the Microsoft Knowledge Base:
247073� CustWBC.exe: ATL HTML Control Customizes the WebBrowser Control
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

↑ Back to the top


Keywords: KB274202, kbwebbrowser, kbsbnworkshop, kbhowto

↑ Back to the top

Article Info
Article ID : 274202
Revision : 3
Created on : 5/11/2006
Published on : 5/11/2006
Exists online : False
Views : 550