【200顶着有分,急】WebBrowser控件编辑模式CTRL+Z失效的问题 或 怎样禁止弹出文档已修改对话框?
我要疯,大侠救命啊…
导致Ctrl + Z失效的原因由以下2点连锁引发而导致:
1、为了解决 WebBrowser 控件导航时弹出“保存对话框”,使用了 this.webBrowser.Document.OpenNew(true); // 防止 弹出保存对话框, 该方法指示新的文本改变将会在新窗口中打开。
2、 由原因1导致 webBrowser 控件的编辑模式失效, 表面上看上去还是可以编辑的,但实际上新窗口内部已经不支持编辑了。
这里涉及到了WebBrowser控件的特殊性,它是由三层控件嵌套而成的,外面的两层是大概负责容器、 及 响应用户操作的, 而最内层的则是承载HTML标记,并通过渲染引擎展示HTML内容。当使用webBrowser.Document.OpenNew(true); 方法时,最内层控件应该是一个新的实例, 表面上看上去还是可以编辑的,但实际上内部的新窗口已经不支持编辑了,进而导致了Ctrl + Z的失效![这里也只是一个推断,顺求大侠们佐证…]
测试代码如下:
public partial class FrmTest : Form
{
// 界面上有一个WebBrowser 和 4个Button
private string strUrl = "http://www.cnblogs.com/08shiyan";
public FrmTest()
{
InitializeComponent();
}
/// <summary>
/// 编辑模式
/// </summary>
public void EditMode()
{
if (this.webBrowser1.Document != null)
{
mshtml.IHTMLDocument2 doc = this.webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;
doc.designMode = "on";
}
}
/// <summary>
/// 启用浏览模式
/// </summary>
public void BrowseMode()
{
if (this.webBrowser1.Document != null)
{
mshtml.IHTMLDocument2 doc = this.webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;
doc.designMode = "off";
}
}
// 请确保该按钮是应用程序启动后第一次被点击
private void button1_Click(object sender, EventArgs e)
{
this.webBrowser1.DocumentText = string.Empty;
this.webBrowser1.Document.Write(string.Format("<BODY>{0}我的誓言博客2</BODY>", this.strUrl));
this.EditMode();
this.webBrowser1.Document.OpenNew(true);
this.webBrowser1.Document.Write(string.Format("<BODY>{0}我的誓言博客2</BODY>", this.strUrl));
// 注意此时Ctrl + Z 撤销操作将会失效
}
// 请确保该按钮是应用程序启动后第一次被点击
private void button2_Click(object sender, EventArgs e)
{
this.webBrowser1.DocumentText = string.Empty;
this.webBrowser1.Document.Write(string.Format("<BODY>{0}我的誓言博客2</BODY>", this.strUrl));
this.EditMode();
this.webBrowser1.Document.OpenNew(true);
this.webBrowser1.Document.Write(string.Format("<BODY>{0}我的誓言博客2</BODY>", this.strUrl));
this.EditMode(); // 与button1的差别是再次启用编辑模式 // 注意此时Ctrl + Z 撤销操作也会失效
}
// 请确保该按钮是应用程序启动后第一次被点击
private void button3_Click(object sender, EventArgs e)
{
this.webBrowser1.DocumentText = string.Empty;
this.webBrowser1.Document.Write(string.Format("<BODY>{0}我的誓言博客2</BODY>", this.strUrl));
this.EditMode();
this.webBrowser1.Document.OpenNew(true);
this.webBrowser1.Document.Write(string.Format("<BODY>{0}我的誓言博客2</BODY>", this.strUrl));
this.BrowseMode(); // 与button2 的区别是 先关闭编辑模式,再启用编辑模式
this.EditMode();
// 此时 Ctrl + Z 可以使用 ,但此时this.webBrowser1.Document.Body 为null了,杯具中、还是不能解决,问题似乎陷入死循环了。
}
// 重启应用程序
private void button4_Click(object sender, EventArgs e)
{
Application.Restart();
}
}
ZaiDuXinLing:你好!
截至 2011-03-18 14:53:37 前:
你已发帖 4 个,未结贴 0 个;
结贴率为: 100.00%
当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html
如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖
如何给自己的回帖中也加上签名?
http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
[解决办法]
别处看的:
//在浏览器完成操作时,使AllowNavigation = false
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.AllowNavigation = false;
}
在设置WebBrowser.DocumentText 的值的前面加下以下两行代码
//这两行是屏蔽改变WebBrowser值时,弹出的如上面所说的那个对话框
webBrowser1.AllowNavigation = true;
webBrowser1.Document.OpenNew(true);
[解决办法]