追踪导致 WebBrowser控件 编辑模式下 Ctrl + Z 失效的原因 【求大侠佐证,这算不算是微软的Bug呢?】
导致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 可以使用
}
// 重启应用程序
private void button4_Click(object sender, EventArgs e)
{
Application.Restart();
}
}
{
// 界面上有一个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 可以使用
}
// 重启应用程序
private void button4_Click(object sender, EventArgs e)
{
Application.Restart();
}
}
根据以上得出结论:
在“编辑模式”下: this.webBrowser.Document.OpenNew(true); 方法会打开一个新的“内部窗口”,而新窗口中“编辑模式”出现问题,导致Ctrl + Z ,Ctrl +Y操作失效。 此时 需要先关闭 “编辑模式” 然后再打开“编辑模式” Ctrl + Z, Ctrl +Y 才能正常使用。
求佐证。。。
原创 转载请标明出处: http://www.cnblogs.com/08shiyan
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?