先说实在的:最终解决办法是实现IDocHostUIHandler接口,在GetHostInfo方法里解决,但“实现接口”意味着QueryInterface、AddRef、Release三个方法必须实现,所以相关代码是:
class
CMyWebBrowser : public IDocHostUIHandler
{
public:
CMyWebBrowser() { m_cRef = 0; }
virtual ~CMyWebBrowser()
{}
LONG
m_cRef;
private:
HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID riid, void **ppvObject)
{
HRESULT result =
S_OK;
if
(IsBadWritePtr(ppvObject,
sizeof(LPVOID)))
result = E_INVALIDARG;
if (result == S_OK)
{
*ppvObject =
NULL;
if ( IsEqualIID( riid, IID_IUnknown )
)
*ppvObject =
this;
// else
if ( IsEqualIID( riid, IID_IOleClientSite )
)
//
*ppvObject = (IOleClientSite *)
this;
else
if ( IsEqualIID( riid, IID_IDocHostUIHandler )
)
*ppvObject = (IDocHostUIHandler *)
this;
else
result = E_NOINTERFACE;
}
if (result ==
S_OK)
this->AddRef();
return result;
}
ULONG
STDMETHODCALLTYPE AddRef(){
InterlockedIncrement(&m_cRef);
return m_cRef;
}
ULONG
STDMETHODCALLTYPE Release() {
ULONG ulRefCount =
InterlockedDecrement(&m_cRef);
return ulRefCount;
}
HRESULT STDMETHODCALLTYPE EnableModeless(BOOL fEnable) { return S_OK;
}
HRESULT STDMETHODCALLTYPE
FilterDataObject(IDataObject *pDO,IDataObject **ppDORet){return
S_OK;}
HRESULT STDMETHODCALLTYPE
GetDropTarget(IDropTarget *pDropTarget, IDropTarget **ppDropTarget){return
S_OK;}
HRESULT STDMETHODCALLTYPE GetExternal(IDispatch
**ppDispatch){return S_OK;}
HRESULT STDMETHODCALLTYPE
GetOptionKeyPath(LPOLESTR *pchKey,DWORD dw){return
S_OK;}
STDMETHOD(GetHostInfo)(DOCHOSTUIINFO *pInfo)
{
pInfo->cbSize =
sizeof(DOCHOSTUIINFO);
pInfo->dwFlags |=
DOCHOSTUIFLAG_NO3DBORDER;
pInfo->dwDoubleClick =
DOCHOSTUIDBLCLK_DEFAULT;
pInfo->pchHostCss = 0;
pInfo->pchHostNS = 0;
return S_OK;
}
HRESULT
STDMETHODCALLTYPE HideUI(VOID){return S_OK;}
HRESULT
STDMETHODCALLTYPE OnDocWindowActivate(BOOL fActivate){return
S_OK;}
HRESULT STDMETHODCALLTYPE
OnFrameWindowActivate(BOOL fActivate){return S_OK;}
HRESULT STDMETHODCALLTYPE ResizeBorder(LPCRECT prcBorder,IOleInPlaceUIWindow
*pUIWindow,BOOL fFrameWindow){return S_OK;}
HRESULT
STDMETHODCALLTYPE ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown
*pcmdtReserved,IDispatch *pdispReserved){return S_OK;}
HRESULT STDMETHODCALLTYPE ShowUI(DWORD
dwID,
IOleInPlaceActiveObject
*pActiveObject,
IOleCommandTarget
*pCommandTarget,
IOleInPlaceFrame
*pFrame,
IOleInPlaceUIWindow
*pDoc
){return
S_OK;}
HRESULT STDMETHODCALLTYPE
TranslateAccelerator(LPMSG lpMsg,
const GUID *pguidCmdGroup,
DWORD
nCmdID
)
{return S_OK;}
HRESULT STDMETHODCALLTYPE
TranslateUrl(DWORD dwTranslate,
OLECHAR *pchURLIn,
OLECHAR
**ppchURLOut
)
{return S_OK;}
HRESULT
STDMETHODCALLTYPE UpdateUI(VOID) {return S_OK;}
};
1.要写的更通用一点,就是上述类就这么些内容,然后WEBBrowser从这个类继承。
2.除了实现这个接口外,别忘了SetUIHandler,这个不难查到,这里就不写了。
牢骚一下:项目中用到WEBBrowser,由于项目组中只有我一个人做过WEB,所以这玩意一直是我维护,最近由于我给里面的Html加了DocType,导致WEBBrowser出现了3D边框,不管是css的border:0还是body的leftmargin和topmargin,都不管用,查了很多资料(主要是google外国人的),都说实现IDocHostUIHandler接口,在GetHostInfo方法里能干掉3D边框,但我按照网上做的,结果要么报错,要么GetHostInfo不走,郁闷了我,折腾了两天,最后几乎不懂COM的我还是觉得QueryInterface等3个方法要实现,结果去查,果然如此,实现之后,搞定~~~
C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口),布布扣,bubuko.com