WebBrowser控件中屏蔽弹出脚本错误对话框

xingyun86 2022-12-26 429

WebBrowser控件中屏蔽弹出脚本错误对话框

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

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

不过要注意的是:

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

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

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;
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回