首先先实现IDocHostUIHandlerDispatch接口
--------------------------------------------------------------------------------------------class ATL_NO_VTABLE CMyUIHandler : public CComObjectRootEx, public CComCoClass , public IDispatchImpl {public: CComQIPtr m_spExtDispatch;public: CMyUIHandler() { }DECLARE_PROTECT_FINAL_CONSTRUCT()BEGIN_COM_MAP(CMyUIHandler) COM_INTERFACE_ENTRY(IDocHostUIHandlerDispatch) COM_INTERFACE_ENTRY(IDispatch)END_COM_MAP()// IXmnWrapperUIHandlerpublic: // Do not show the context menu STDMETHOD (ShowContextMenu) ( /* [in] */ DWORD dwID, /* [in] */ DWORD x, /* [in] */ DWORD y, /* [in] */ IUnknown *pcmdtReserved, /* [in] */ IDispatch *pdispReserved, /* [retval][out] */ HRESULT *dwRetVal) { *dwRetVal = S_OK; return S_OK; } // Mark all the other methods as not implemented STDMETHOD (GetHostInfo) ( /* [out][in] */ DWORD *pdwFlags, /* [out][in] */ DWORD *pdwDoubleClick) { return E_NOTIMPL; } STDMETHOD (ShowUI) ( /* [in] */ DWORD dwID, /* [in] */ IUnknown *pActiveObject, /* [in] */ IUnknown *pCommandTarget, /* [in] */ IUnknown *pFrame, /* [in] */ IUnknown *pDoc, /* [retval][out] */ HRESULT *dwRetVal) { return E_NOTIMPL; } STDMETHOD (HideUI) (void) { return E_NOTIMPL; } STDMETHOD (UpdateUI) (void) { return E_NOTIMPL; } STDMETHOD (EnableModeless) ( /* [in] */ VARIANT_BOOL fEnable) { return E_NOTIMPL; } STDMETHOD (OnDocWindowActivate) ( /* [in] */ VARIANT_BOOL fActivate) { return E_NOTIMPL; } STDMETHOD (OnFrameWindowActivate) ( /* [in] */ VARIANT_BOOL fActivate) { return E_NOTIMPL; } STDMETHOD (ResizeBorder) ( /* [in] */ long left, /* [in] */ long top, /* [in] */ long right, /* [in] */ long bottom, /* [in] */ IUnknown *pUIWindow, /* [in] */ VARIANT_BOOL fFrameWindow) { return E_NOTIMPL; } STDMETHOD (TranslateAccelerator) ( /* [in] */ DWORD hWnd, /* [in] */ DWORD nMessage, /* [in] */ DWORD wParam, /* [in] */ DWORD lParam, /* [in] */ BSTR bstrGuidCmdGroup, /* [in] */ DWORD nCmdID, /* [retval][out] */ HRESULT *dwRetVal) { return E_NOTIMPL; } STDMETHOD (GetOptionKeyPath) ( /* [out] */ BSTR *pbstrKey, /* [in] */ DWORD dw) { return E_NOTIMPL; } STDMETHOD (GetDropTarget) ( /* [in] */ IUnknown *pDropTarget, /* [out] */ IUnknown **ppDropTarget) { return E_NOTIMPL; } STDMETHOD (GetExternal) ( /* [out] */ IDispatch **ppDispatch) { return m_spExtDispatch.CopyTo (ppDispatch); } STDMETHOD (TranslateUrl) ( /* [in] */ DWORD dwTranslate, /* [in] */ BSTR bstrURLIn, /* [out] */ BSTR *pbstrURLOut) { return E_NOTIMPL; } STDMETHOD (FilterDataObject) ( /* [in] */ IUnknown *pDO, /* [out] */ IUnknown **ppDORet) { return E_NOTIMPL; }};
--------------------------------------------------------------------------------------------
然后通过SetExternalUIHandler将IDocHostUIHandlerDispatch指针传进CAxHostWindowCComPtrpunkCtrl; CComQIPtr pWB2; CComVariant v; CRect rc(0,0,640,480); m_wndIE.Create ( *this, rc, _T(""), WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE ); //m_wndIE.CreateControlEx ( L"{8856F961-340A-11D0-A96B-00C04FD705A2}", NULL, // NULL, &punkCtrl ); m_wndIE.CreateControlEx ( L"Shell.Explorer", NULL, NULL, &punkCtrl ); pWB2 = punkCtrl; if ( pWB2 ) pWB2->Navigate ( CComBSTR("http://www.microsoft.com"), &v, &v, &v, &v ); CComPtr spUnk; HRESULT hr = CMyUIHandler::_CreatorClass::CreateInstance(NULL, IID_IUnknown, (void**)&spUnk); ATLASSERT(SUCCEEDED(hr)); CComQIPtr spUI = spUnk; m_wndIE.SetExternalUIHandler(spUI);