当IE浏览器遇到脚本错误时浏览器,左下 角会出现一个黄色图标,点击可以查看脚本错误的详细信息,并不会有弹出的错误信息框。当我们使用 WebBrowser控件时有错误信息框弹出,这样程序显的很不友好,而且会让一些自动执行的程序暂停。我看到有人采取的解决方案是做一个窗体杀手程序来 关闭弹出的窗体。今天探讨的方法是从控件解决问题。

1、SHDocVw.dll

在COM时代我们使用的WebBrowser控件是SHDocVw.dll。屏蔽错误信息的方法很简单使用下面的一句就可以搞定。

view plaincopy to clipboardprint?
WebBrowser1.Silent = true; 
WebBrowser1.Silent = true;

2、.Net中

在.Net中提供了托管的WebBrowser可供我们使用,当然我们仍然可以在.Net中使用COM组建SHDocVw.dll,如果使用SHDocVw.dll
处理错误方式和上面的方法一样。但如果我们是使用.Net组件如何解决这个问题呢?

这个组件给我们提供了一个方法ScriptErrorsSuppressed 。但是在.net framework2.0中他是不起作用的,据说在低版本中使用如下的方式解决

view plaincopy to clipboardprint?
webBrowser1.ScriptErrorsSuppressed = true; 
webBrowser1.ScriptErrorsSuppressed = true;


WebBrowser 控件 ScriptErrorsSuppressed
设置为True,可禁止弹出脚本错误对话框,ScriptErrorsSuppressed属性是对其基础COM控件的Silent属性的封装,因此设置
ScriptErrorsSuppressed属性和设置其基础COM控件的Slient属性是效果一样的,这一点通过反编译
System.Windows.Forms程序集可以证实。

为了解决这个问题,有的人专门从WebBrowser派生出一个新类,然后重写了AttachInterfaces方法,其实也是没有必要的,效果和直接设置ScriptErrorsSuppressed属性相同。

不过要注意的是:
ScriptErrorsSuppressed 设置为True会禁用所有的对话框,比如提示Activex下载、执行以及安全登录等对话框。

如果不想禁止除脚本错误之外的对话框,请使用MSDN上的代码示例:

view plaincopy to clipboardprint?
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  
{  
    ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);  
}  
 
private void Window_Error(object sender, HtmlElementErrorEventArgs e)  
{  
    // Ignore the error and suppress the error dialog box.   
    e.Handled = true;  

private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
}

private void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
    // Ignore the error and suppress the error dialog box.
    e.Handled = true;
}

3、上面的方法对于多个框架嵌套等等的情形还是不能很好的解决

为了彻底解决这个问题,我们借助AxWebBrowser来解决WebBrowser的问题。

我们定义一个自己的类,他的父类是WebBrowser,以后使用这个类就可以了。在这个类的定义中需要引用SHDocVw。
view plaincopy to clipboardprint?
class EWebBrowser : System.Windows.Forms.WebBrowser  
{  
    SHDocVw.IWebBrowser2 Iwb2;  
 
    protected override void AttachInterfaces(object nativeActiveXObject)  
    {  
        Iwb2 = (SHDocVw.IWebBrowser2) nativeActiveXObject;  
        Iwb2.Silent = true;  
        base.AttachInterfaces(nativeActiveXObject);  
    }  
 
    protected override void DetachInterfaces()  
    {  
        Iwb2 = null;  
        base.DetachInterfaces();  
    }  

class EWebBrowser : System.Windows.Forms.WebBrowser
{
    SHDocVw.IWebBrowser2 Iwb2;

protected override void AttachInterfaces(object nativeActiveXObject)
    {
        Iwb2 = (SHDocVw.IWebBrowser2) nativeActiveXObject;
        Iwb2.Silent = true;
        base.AttachInterfaces(nativeActiveXObject);
    }

protected override void DetachInterfaces()
    {
        Iwb2 = null;
        base.DetachInterfaces();
    }
}

I、项目中添加Micrsoft.mshtml引用
using mshtml;  
 
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)  
{  
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;  
    vDocument.parentWindow.execScript(  
        "function alert(str){if(str=='zswang')confirm(str);}", "javaScript");  
}  
 
//frame结构  
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)  
{  
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;  
    foreach (IHTMLElement vElement in vDocument.all)  
        if (vElement.tagName.ToUpper() == "FRAME")  
        {  
            IHTMLFrameBase2 vFrameBase2 = vElement as IHTMLFrameBase2;  
            vFrameBase2.contentWindow.execScript(  
                "function alert(str){confirm('[' + str + ']');}", "javaScript");  
        }  

using mshtml;

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
    vDocument.parentWindow.execScript(
        "function alert(str){if(str=='zswang')confirm(str);}", "javaScript");
}

//frame结构
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
    foreach (IHTMLElement vElement in vDocument.all)
        if (vElement.tagName.ToUpper() == "FRAME")
        {
            IHTMLFrameBase2 vFrameBase2 = vElement as IHTMLFrameBase2;
            vFrameBase2.contentWindow.execScript(
                "function alert(str){confirm('[' + str + ']');}", "javaScript");
        }
}

II、屏蔽其它窗口
   
(wb.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 += new
DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);//wb
是一个Webbrowser控件  
 
//屏蔽一些弹出窗口  
void wb_NavigateComplete2(object pDisp, ref object URL)  
{  
    mshtml.IHTMLDocument2 doc = (wb.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;  
    doc.parentWindow.execScript("window.alert=null", "javascript");  
    doc.parentWindow.execScript("window.confirm=null", "javascript");  
    doc.parentWindow.execScript("window.open=null", "javascript");  
    doc.parentWindow.execScript("window.showModalDialog=null", "javascript");  
    doc.parentWindow.execScript("window.close=null", "javascript");  

           
(wb.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 += new
DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);//wb
是一个Webbrowser控件

//屏蔽一些弹出窗口
        void wb_NavigateComplete2(object pDisp, ref object URL)
        {
            mshtml.IHTMLDocument2 doc = (wb.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;
            doc.parentWindow.execScript("window.alert=null", "javascript");
            doc.parentWindow.execScript("window.confirm=null", "javascript");
            doc.parentWindow.execScript("window.open=null", "javascript");
            doc.parentWindow.execScript("window.showModalDialog=null", "javascript");
            doc.parentWindow.execScript("window.close=null", "javascript");
        }

III、自动确定弹出对话框

Q:winform中如何实现自动点击webbrowser弹出对话框中的确定按钮

A:

view plaincopy to clipboardprint?
 //using mshtml;  
//using SHDocVw;  
private void Form1_Load(object sender, EventArgs e)  
{  
    this.webBrowser1.Navigate("http://www.a60.com.cn");  
    SHDocVw.WebBrowser wb = this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser;  
    wb.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);  
       
}  
 
void wb_NavigateComplete2(object pDisp, ref object URL)  
{  
    mshtml.IHTMLDocument2 doc = (this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;  
    doc.parentWindow.execScript("function alert(str){return ''}", "javascript");  

         //using mshtml;
        //using SHDocVw;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate("http://www.a60.com.cn");
            SHDocVw.WebBrowser wb = this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser;
            wb.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);
            
        }

void wb_NavigateComplete2(object pDisp, ref object URL)
        {
           
mshtml.IHTMLDocument2 doc = (this.webBrowser1.ActiveXInstance as
SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;
            doc.parentWindow.execScript("function alert(str){return ''}", "javascript");
        }

C# 禁止 Webbrowser 控件的弹出脚本错误对话框的更多相关文章

  1. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  2. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  3. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  4. 控件(弹出类): ToolTip, Popup, PopupMenu

    示例1.ToolTip 的示例Controls/FlyoutControl/ToolTipDemo.xaml <Page x:Class="Windows10.Controls.Fly ...

  5. ocx控件避免弹出警告的类--2

    本文与 OCX控件避免弹出安全警告的类 http://www.cnblogs.com/lidabo/archive/2013/03/26/2981852.html 有些类似,只不过增加了几行代码(红色 ...

  6. 小程序中点击input控件键盘弹出时placeholder文字上移

    最近做的一个小程序项目中,出现了点击input控件键盘弹出时placeholder文字上移,刚开始以为是软键盘弹出布局上移问题是传说中典型的fixed 软键盘顶起问题,因此采纳了网上搜到的" ...

  7. WPF中禁止WebBrowser控件打开新窗口

    一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...

  8. WPF 设置WebBrowser控件不弹脚本错误提示框

    using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.N ...

  9. OCX控件避免弹出安全警告的类

    1.要加一个头文件:         #include <objsafe.h>2.在控件头文件中加入: 1 DECLARE_INTERFACE_MAP()2 BEGIN_INTERFACE ...

随机推荐

  1. Area - POJ 1265(pick定理求格点数+求多边形面积)

    题目大意:以原点为起点然后每次增加一个x,y的值,求出来最后在多边形边上的点有多少个,内部的点有多少个,多边形的面积是多少. 分析: 1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其 ...

  2. 我的ubuntu配置

    每次装系统都是非常蛋疼的过程,新装的系统还是要配置一下的 首先安装google拼音 sudo apt-get install fcitx fcitx-googlepinyin 然后按装numix主题 ...

  3. MongoDB 复制集 (一) 成员介绍

       一 MongoDB 复制集简介          MongoDB的复制机制主要分为两种:          Master-Slave    (主从复制)      这个已经不建议使用       ...

  4. DevExpress 用户控件 分页(下)

    分页控件调用 (1)初始化时: this.pageCtrl1.pageSize = 4; (2)数据绑定时: 从数据库中获取实时的 Public void LoadData(){ //这是只写有关分页 ...

  5. SSH返回Json格式的数据

      在开发中我们经常遇到客户端和后台数据的交互,使用比较多的就是json格式了.在这里以简单的Demo总结两种ssh返回Json格式的数据 项目目录如下 主要是看 上图选择的部分 WebRoot里面就 ...

  6. C# 动态创建出来的窗体间的通讯 delegate1

    附件 http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate01.rar 需要每个窗体是独立存在,禁止相与引用窗体 这样干净并且可以反 ...

  7. ngnix apache tomcat集群负载均衡配置

    http://w.gdu.me/wiki/Java/tomcat_cluster.html 参考: Tomcat与Apache或Nginx的集群负载均衡设置: http://huangrs.blog. ...

  8. HDU-4879-ZCC loves march(map+set+并查集)

    Description On a m*m land stationed n troops, numbered from 1 to n. The i-th troop's position can be ...

  9. cocos2dx游戏项目总结(持续更新)

    1.在一个layer里面,尽量只使用一种按钮类型.如MenuItem or CCControlButton.因为这两种按钮的优先级不同,在使用过程中会互相影响到事件触发的先后顺序. 2.细节的问题要一 ...

  10. NDK开发之访问域

    Java有两类域,实例域和静态域.类的每个实例都有自己的实例域副本,而一个类的所有实例共享一个静态域(Java SE基础). JNI提供了相应的函数来访问这两类域,总体步骤是这样的: 1.通过对象引用 ...