环境: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");
        }
使用后:无效。这个万恶的保存对话框,就像牛皮癣一样顽固而恶心,肯请高手现身,教小弟绝杀之方法!先谢了!

解决方案 »

  1.   

    void   webBrowser1_Navigating(object   sender,   WebBrowserNavigatingEventArgs   e) 

          webBrowser1.Document.Write("<script> function   showModalDialog   {   return;} </script> "); 

      

  2.   

    楼上朋友:
    不使用OpenNew,还是会出异常的
      

  3.   

    换个马甲做下总结:一楼列举的三种方法通通无效。而      
    HtmlDocument doc = ((ExtendWebBrowser)sender).Document.OpenNew(true);
    doc.Write("<script> function showModalDialog {return;} </script>");这里其实只要执行一个OpenNew,就能达到目的(屏蔽对话框),根本不需要执行后面的JS代码。但这也仅仅是在屏蔽保存对话框上取得一点进步,异常问题还是解决不了。总之:OpenNew或Write创建一个新页,会导致我一楼所说的异常。考虑了下项目实际,我决定在navigate后加上webbrowser1.AllowNavigation = false;
    这样即能屏蔽保存对话框,又不会出异常。缺点是webbrowser控件不能再异航到其它页了。我项目中webbrowser控件在转到编辑模式后,事实上不需要再导航,于是决定采用该方法。
      

  4.   

    楼上朋友:webbrowser控件没有提供一个不弹提示框的保存函数。哪位大虾有好的解决办法?
      

  5.   

    我现在采用的方法是:延时设置webbrowser控件的编辑模式,在文档加载完毕后再根据需要设为编辑模式
      

  6.   

    很巧,我刚用该控件完成一件任务,现贴关键代码供楼主参考:        /// <summary>
            /// 获取和设置当前的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;
                }
            }