本文主要是介绍WebBrowser控件如何屏蔽javascript错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
WebBrowser 如何屏蔽javascript错误,我搜啊搜啊,搜到了
http://www.cnblogs.com/zhangqingping/archive/2009/06/16/1504260.html 和 http://support.microsoft.com/kb/261003
http://www.codeproject.com/Articles/4003/Popup-Window-Blocker
#pragma once#include <mshtmhst.h>class CNoScriptErrorHelper: public IDocHostUIHandler, public IOleCommandTarget
{public: HRESULT STDMETHODCALLTYPE SetHandler(CComPtr<IWebBrowser2> spWebBrowser){HRESULT result = E_NOINTERFACE;CComPtr<IDispatch> spDisp;HRESULT hr = spWebBrowser->get_Document(&spDisp);if (SUCCEEDED(hr) && spDisp){// If this is not an HTML document (e.g., it's a Word doc or a PDF), don't sink.CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML(spDisp);if (spHTML){// Get pointers to default interfacesCComQIPtr<IOleObject, &IID_IOleObject> spOleObject(spDisp);if (spOleObject){CComPtr<IOleClientSite> spClientSite;hr = spOleObject->GetClientSite(&spClientSite);if (SUCCEEDED(hr) && spClientSite){m_spDefaultDocHostUIHandler = spClientSite;m_spDefaultOleCommandTarget = spClientSite;}}// Set this class to be the IDocHostUIHandlerCComQIPtr<ICustomDoc, &IID_ICustomDoc> spCustomDoc(spDisp);if (spCustomDoc) {spCustomDoc->SetUIHandler(this);result = S_OK;}}} return result;}
public: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_IDispatch ) )*ppvObject = (IDispatch *) this;else if ( IsEqualIID( riid, IID_IDocHostUIHandler ) )*ppvObject = (IDocHostUIHandler *) this;else if ( IsEqualIID( riid, IID_IOleCommandTarget ) )*ppvObject = (IOleCommandTarget *) this;elseresult = 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);if(m_cRef == 0){delete this;}return ulRefCount;}STDMETHOD(ShowContextMenu)(DWORD dwID, POINT FAR* ppt, IUnknown FAR* pcmdTarget, IDispatch FAR* pdispReserved){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->ShowContextMenu(dwID, ppt, pcmdTarget, pdispReserved);return S_FALSE;}STDMETHOD(ShowUI)(DWORD dwID, IOleInPlaceActiveObject FAR* pActiveObject,IOleCommandTarget FAR* pCommandTarget,IOleInPlaceFrame FAR* pFrame,IOleInPlaceUIWindow FAR* pDoc){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->ShowUI(dwID, pActiveObject, pCommandTarget, pFrame, pDoc);return S_FALSE;}STDMETHOD(GetHostInfo)(DOCHOSTUIINFO FAR *pInfo){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->GetHostInfo(pInfo);return S_OK;}STDMETHOD(HideUI)(void){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->HideUI();return S_OK;}STDMETHOD(UpdateUI)(void){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->UpdateUI();return S_OK;}STDMETHOD(EnableModeless)(BOOL fEnable){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->EnableModeless(fEnable);return S_OK;}STDMETHOD(OnDocWindowActivate)(BOOL fActivate){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->OnDocWindowActivate(fActivate);return S_OK;}STDMETHOD(OnFrameWindowActivate)(BOOL fActivate){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->OnFrameWindowActivate(fActivate);return S_OK;}STDMETHOD(ResizeBorder)(LPCRECT prcBorder, IOleInPlaceUIWindow FAR* pUIWindow, BOOL fFrameWindow){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->ResizeBorder(prcBorder, pUIWindow, fFrameWindow);return S_OK;}STDMETHOD(TranslateAccelerator)(LPMSG lpMsg, const GUID FAR* pguidCmdGroup, DWORD nCmdID){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->TranslateAccelerator(lpMsg, pguidCmdGroup, nCmdID);return E_NOTIMPL;}STDMETHOD(GetOptionKeyPath)(LPOLESTR FAR* pchKey, DWORD dw){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->GetOptionKeyPath(pchKey, dw);return E_FAIL;}STDMETHOD(GetDropTarget)(IDropTarget* pDropTarget, IDropTarget** ppDropTarget){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->GetDropTarget(pDropTarget, ppDropTarget);return S_OK;}STDMETHOD(GetExternal)(IDispatch** ppDispatch){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->GetExternal(ppDispatch);return S_FALSE;}STDMETHOD(TranslateUrl)(DWORD dwTranslate, OLECHAR* pchURLIn, OLECHAR** ppchURLOut){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->TranslateUrl(dwTranslate, pchURLIn, ppchURLOut);return S_FALSE;}STDMETHOD(FilterDataObject)(IDataObject* pDO, IDataObject** ppDORet){if (m_spDefaultDocHostUIHandler)return m_spDefaultDocHostUIHandler->FilterDataObject(pDO, ppDORet);return S_FALSE;}//// IOleCommandTarget//STDMETHOD(QueryStatus)(/*[in]*/ const GUID *pguidCmdGroup, /*[in]*/ ULONG cCmds,/*[in,out][size_is(cCmds)]*/ OLECMD *prgCmds,/*[in,out]*/ OLECMDTEXT *pCmdText){return m_spDefaultOleCommandTarget->QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText);}STDMETHOD(Exec)(/*[in]*/ const GUID *pguidCmdGroup,/*[in]*/ DWORD nCmdID,/*[in]*/ DWORD nCmdExecOpt,/*[in]*/ VARIANTARG *pvaIn,/*[in,out]*/ VARIANTARG *pvaOut){if (nCmdID == OLECMDID_SHOWSCRIPTERROR){// Don't show the error dialog, but// continue running scripts on the page.(*pvaOut).vt = VT_BOOL;(*pvaOut).boolVal = VARIANT_TRUE;return S_OK;}return m_spDefaultOleCommandTarget->Exec(pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn, pvaOut);}
private:// Default interface pointersCComQIPtr<IDocHostUIHandler, &IID_IDocHostUIHandler> m_spDefaultDocHostUIHandler;CComQIPtr<IOleCommandTarget, &IID_IOleCommandTarget> m_spDefaultOleCommandTarget;LONG m_cRef;
};
使用方法:
class CBrowserView: public CWindowImpl<CBrowserView, CAxWindow>, public IDispEventImpl<_nDispatchID, CBrowserView, &DIID_DWebBrowserEvents2>
{
public:DECLARE_WND_SUPERCLASS(_T("BrowserView"), CAxWindow::GetWndClassName())CNoScriptErrorHelper m_helper;BEGIN_SINK_MAP(CBrowserView)SINK_ENTRY_EX(_nDispatchID, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnEventDocumentComplete)END_SINK_MAP()void __stdcall OnEventDocumentComplete(IDispatch* pDisp, VARIANT* URL){CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> iwebBrowser(pDisp);m_helper.SetHandler(iwebBrowser);}
// Message map and handlersBEGIN_MSG_MAP(CBrowserView)MESSAGE_HANDLER(WM_CREATE, OnCreate)MESSAGE_HANDLER(WM_DESTROY, OnDestroy)END_MSG_MAP()LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/){LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);// Connect eventsCComPtr spWebBrowser2;HRESULT hRet = QueryControl(IID_IWebBrowser2, (void**)&spWebBrowser2);if(SUCCEEDED(hRet)){if(FAILED(DispEventAdvise(spWebBrowser2, &DIID_DWebBrowserEvents2)))ATLASSERT(FALSE);}// Set host flag to indicate that we handle themesCComPtr spHost;hRet = QueryHost(IID_IAxWinAmbientDispatch, (void**)&spHost);if(SUCCEEDED(hRet)){const DWORD _DOCHOSTUIFLAG_THEME = 0x40000;hRet = spHost->put_DocHostFlags(DOCHOSTUIFLAG_NO3DBORDER | _DOCHOSTUIFLAG_THEME);ATLASSERT(SUCCEEDED(hRet));}return lRet;}LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled){// Disconnect eventsCComPtr spWebBrowser2;HRESULT hRet = QueryControl(IID_IWebBrowser2, (void**)&spWebBrowser2);if(SUCCEEDED(hRet))DispEventUnadvise(spWebBrowser2, &DIID_DWebBrowserEvents2);bHandled=FALSE;return 1;}
};
CBrowserView 创建窗口的时候用:
CBrowserView m_view;
m_view.Create(parentHWND, rcDefault, L"http://www.iteye.com", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL);
这篇关于WebBrowser控件如何屏蔽javascript错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!