如何把IE9的WebBrowser控件纳入标准?
我正在使用自动化(即 COM 自动化)在 Internet Explorer 中显示一些 HTML (9):
ie = CoInternetExplorer.Create;
ie.Navigate2("about:blank");
webDocument = ie.Document;
webDocument.Write(szSourceHTML);
webDocument.Close();
ie.Visible = True;
Internet Explorer 出现,显示我的 html,其开头为:
<!DOCTYPE html>
<HTML>
<HEAD>
...
注意: html5 标准模式选择加入文档类型
html
除非该文档不是 ie9 标准模式;它处于 ie8 标准模式:
如果我先将 html 保存到我的计算机上:
然后查看 那个 html 文档,IE 进入标准模式:
png" alt="alt text">
我的问题是如何更新我的 SpawnIEWithSource(String html)
函数以使浏览器进入标准模式?
void SpawnIEWithSource(String html)
{
Variant ie = CoInternetExplorer.Create();
ie.Navigate2("about:blank");
webDocument = ie.Document;
webDocument.Write(html);
webDocument.Close();
ie.Visible = true;
}
编辑:一个更详细、更难理解或可读的代码示例,无助于进一步解决问题可能是:
IWebBrowser2 ie;
CoCreateInstance(CLASS_InternetExplorer, null, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_WebBrowser2, ie);
ie.AddRef();
ie.Navigate2("about:blank");
IHtmlDocument doc;
dispDoc = ie.Document;
dispDoc.AddRef();
dispDoc.QueryInterface(IHTMLDocument2, doc);
dispDoc.Release()
doc.Write(html);
doc.Close();
doc.Release();
ie.Visible = true;
ie.Release();
更新
评论者在 ieblog 条目上询问的 使用浏览器模式与文档模式测试网站:
我们能否描述一下当 HTML 内容位于嵌入式 Web 控件中时如何确定文档模式?似乎文档模式的选择不同 - 也许是出于兼容性原因?
MarkSil [MSFT] 回应:
@Thomas:感谢您提出这个问题。 WebBrowser Control 确定文档模式的方式与IE 相同,因为它包含相同的Web 平台(例如,IE 和WebBrowser Control 主机之间有一个共享的mshtml.dll)。 WebBrowser 控件默认为兼容性视图浏览器模式,这意味着默认的文档模式是 IE7。这是一篇博客文章,对此有更多详细信息: blogs.msdn.com/.../more-ie8-extensibility-improvements.aspx。
对此,托马斯回应道:
@MarcSil(回复:WebBrowser 控件)
使用注册表项为 WebControl 选择文档模式的问题在于它适用于整个应用程序。我为 Google SketchUp 编写插件,其中有 WebDialog 窗口来创建 UI - 它只是窗口中的 WebBrowser 控件。但这会导致问题,因为我想为我的 WebBrowser 控件实例强制使用文档模式,而不是为所有 SU 的 WebBrowser 控件作为一个整体。
所以,我的问题是:如何控制 WebBrowser 控件每个实例的文档模式?
i am using automation (i.e. COM automation) to display some HTML in Internet Explorer (9):
ie = CoInternetExplorer.Create;
ie.Navigate2("about:blank");
webDocument = ie.Document;
webDocument.Write(szSourceHTML);
webDocument.Close();
ie.Visible = True;
Internet Explorer appears, showing my html, which starts off as:
<!DOCTYPE html>
<HTML>
<HEAD>
...
Note: the html5 standards-mode opt-in doctype
html
Except that the document is not in ie9 standards mode; it's in ie8 standards mode:
If i save the html to my computer first:
and then view that html document, IE is put into standards mode:
My question is how update my SpawnIEWithSource(String html)
function to throw the browser into standards mode?
void SpawnIEWithSource(String html)
{
Variant ie = CoInternetExplorer.Create();
ie.Navigate2("about:blank");
webDocument = ie.Document;
webDocument.Write(html);
webDocument.Close();
ie.Visible = true;
}
Edit: A more verbose, less understandable or readable code sample, that doesn't help further the question might be:
IWebBrowser2 ie;
CoCreateInstance(CLASS_InternetExplorer, null, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_WebBrowser2, ie);
ie.AddRef();
ie.Navigate2("about:blank");
IHtmlDocument doc;
dispDoc = ie.Document;
dispDoc.AddRef();
dispDoc.QueryInterface(IHTMLDocument2, doc);
dispDoc.Release()
doc.Write(html);
doc.Close();
doc.Release();
ie.Visible = true;
ie.Release();
Update
Commenter asked on the ieblog entry Testing sites with Browser Mode vs. Doc Mode:
Can we get a description of how the document mode is determined when the HTML content is within an embedded webcontrol? Seems to be that the document mode is choosen differently - maybe for compatibility reasons?
MarkSil [MSFT] responded:
@Thomas: Thanks for raising that question. The WebBrowser Control determines the doc mode the same way that IE does because it contains the same web platform (e.g. there is one shared mshtml.dll across IE and WebBrowser Control hosts). The WebBrowser Control does default to the Compatibility View browser mode, which means that the default doc mode is IE7. Here is a blog post with more detail on this: blogs.msdn.com/.../more-ie8-extensibility-improvements.aspx.
To which Thomas responded:
@MarcSil (re: WebBrowser Control)
The problem with using registry entries to select document mode for WebControl is that it applies to the application as a whole. I write plugins for Google SketchUp where you have WebDialog windows to create UIs - it's just a WebBrowser control in a window. But that leads to problems as I want to force a document mode for my instance of the WebBrowser control, not for all of SU's WebBrowser controls as a whole.
So, my question is: how do you control the document mode per instance for a WebBrowser control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过在 html 中设置
或
表示最新版本
Have you tried setting in your html the
or
which means latest version
IE9“版本”的WebBrowser控件与IE8版本一样,实际上是多个浏览器合二为一。与 IE8 版本不同,您可以通过更改文档类型对页面内的渲染模式进行更多控制。当然,要更改浏览器模式,您必须像之前的答案一样设置注册表。这是 FEATURE_BROWSER_EMULATION 的位置:
这是完整的代码集:
网页在IE9中显示
标准模式,无论
!DOCTYPE 指令。
指令在 IE9 模式下显示。
在 IE8 标准模式下显示,
无论 !DOCTYPE 指令如何。
基于标准的 !DOCTYPE 指令
在IE8模式下显示。
基于标准的 !DOCTYPE 指令
以 IE7 标准模式显示。
完整文档:
http://msdn.microsoft.com/en-我们/library/ee330730%28VS.85%29.aspx#browser_emulation
The IE9 "version" of the WebBrowser control, like the IE8 version, is actually several browsers in one. Unlike the IE8 version, you do have a little more control over the rendering mode inside the page by changing the doctype. Of course, to change the browser mode you have to set your registry like the earlier answer. Here is the location of FEATURE_BROWSER_EMULATION:
Here is the complete set of codes:
Webpages are displayed in IE9
Standards mode, regardless of the
!DOCTYPE directive.
directives are displayed in IE9 mode.
displayed in IE8 Standards mode,
regardless of the !DOCTYPE directive.
standards-based !DOCTYPE directives
are displayed in IE8 mode.
standards-based !DOCTYPE directives
are displayed in IE7 Standards mode.
The full docs:
http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation
FEATURE_BROWSER_EMULATION 不适用于 CoInternetSetFeatureEnabled。 INTERNETFEATURELIST 的文档未更新从IE7开始。
由于功能设置位于 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl 下,您也许能够通过注册表 API 挂钩覆盖进程中的值。
FEATURE_BROWSER_EMULATION does not works with CoInternetSetFeatureEnabled. The documentation of INTERNETFEATURELIST is not updated since IE7.
Since the feature setting is under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl you may be able to override the value in your process via a registry API hook.