环境:VS2005 C# XP我用Webbrowser做了一个html编辑器。在编辑状态时,执行Navigate会弹出一个对话框: “该文档已被修改,是否保存修改结果...[是] [否] [取消]” 为了让这家伙消失,我先前使用的方法是:void extendWebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
HtmlDocument doc = ((ExtendWebBrowser)sender).Document.OpenNew(true);
doc.Write("<script> function showModalDialog {return;} </script>");
}
但我发现了一个bug,就是之后往WebBrowser中粘贴网页数据,会出现异常:System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在 System.Windows.Forms.UnsafeNativeMethods.IOleInPlaceActiveObject.TranslateAccelerator(MSG& lpmsg)
在 System.Windows.Forms.WebBrowserBase.PreProcessMessage(Message& msg)我想了想,可能Write这种方法太简单粗暴了,于是网上又找了几个办法:方法1
实现IDocHostShowUI接口,重载ShowMessage函数。
该方法得确能够捕获一些窗口,比如点击网页中"关闭当前页"按钮,往常要弹个警告对话框的,重载后返回S_OK就没有了。遗憾的是,保存对话框对该方法免疫,它照弹不误。方法2void 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");
}
为了使用该方法,我特地给VS2005 C#的webbrowser添加了NavigateComplete2事件,遗憾的是,使用该方法后,保存对话框该弹还是弹。方法3 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
string s = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";
win.execScript(s, "javascript");
}
使用后:无效。这个万恶的保存对话框,就像牛皮癣一样顽固而恶心,肯请高手现身,教小弟绝杀之方法!先谢了!
{
HtmlDocument doc = ((ExtendWebBrowser)sender).Document.OpenNew(true);
doc.Write("<script> function showModalDialog {return;} </script>");
}
但我发现了一个bug,就是之后往WebBrowser中粘贴网页数据,会出现异常:System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在 System.Windows.Forms.UnsafeNativeMethods.IOleInPlaceActiveObject.TranslateAccelerator(MSG& lpmsg)
在 System.Windows.Forms.WebBrowserBase.PreProcessMessage(Message& msg)我想了想,可能Write这种方法太简单粗暴了,于是网上又找了几个办法:方法1
实现IDocHostShowUI接口,重载ShowMessage函数。
该方法得确能够捕获一些窗口,比如点击网页中"关闭当前页"按钮,往常要弹个警告对话框的,重载后返回S_OK就没有了。遗憾的是,保存对话框对该方法免疫,它照弹不误。方法2void 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");
}
为了使用该方法,我特地给VS2005 C#的webbrowser添加了NavigateComplete2事件,遗憾的是,使用该方法后,保存对话框该弹还是弹。方法3 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
string s = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";
win.execScript(s, "javascript");
}
使用后:无效。这个万恶的保存对话框,就像牛皮癣一样顽固而恶心,肯请高手现身,教小弟绝杀之方法!先谢了!
解决方案 »
- 如何控制模式对话框的显示位置
- 如何用vs2005中的c#开发activex
- c#控制iis 填写默认文档问题请教
- C#视频捕捉问题
- C# 写单行代码太长了怎么换行啊
- C#中怎么获取局域网内所有的ip地址呢
- c#+sqlserver2000开发一个小软件,给钱的……急急急急
- 谁知道哪里有《Understanding .NET: A Tutorial and Analysis》电子书下载?请站出来!
- 有没有什么好的办法让DataGird只显示指定的列?急问的说
- 如何把两个TextBox内输入的数字经过数学运算的结果赋給另一个TextBox
- 登录框输入错误后就关闭的错误
- 截取部分图片问题,大哥请进,着急
{
webBrowser1.Document.Write("<script> function showModalDialog { return;} </script> ");
}
不使用OpenNew,还是会出异常的
HtmlDocument doc = ((ExtendWebBrowser)sender).Document.OpenNew(true);
doc.Write("<script> function showModalDialog {return;} </script>");这里其实只要执行一个OpenNew,就能达到目的(屏蔽对话框),根本不需要执行后面的JS代码。但这也仅仅是在屏蔽保存对话框上取得一点进步,异常问题还是解决不了。总之:OpenNew或Write创建一个新页,会导致我一楼所说的异常。考虑了下项目实际,我决定在navigate后加上webbrowser1.AllowNavigation = false;
这样即能屏蔽保存对话框,又不会出异常。缺点是webbrowser控件不能再异航到其它页了。我项目中webbrowser控件在转到编辑模式后,事实上不需要再导航,于是决定采用该方法。
/// 获取和设置当前的Html文本
/// </summary>
[Browsable(true)]
public override string Text
{
get
{
//return webBrowserBody.DocumentText;
if (webBrowserBody.Document == null || webBrowserBody.Document.Body == null)
return "";
if (iLastFocus == 0)
return webBrowserBody.Document.Body.InnerHtml;
else
return txtBrowerBody.Text.Trim();
}
set
{
//webBrowserBody.DocumentText = value.Replace("\r\n", "<br>");
string sValue = value.Replace("\r\n", "");
if (webBrowserBody.Document != null && webBrowserBody.Document.Body != null)
{
webBrowserBody.Document.Body.InnerHtml = sValue;
txtBrowerBody.Text = sValue;
}
else
webBrowserBody.Tag = sValue;
}
}