Html 如何把WebBrowser控件放到IE9中成标准?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4097593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put the WebBrowser control into IE9 into standards?
提问by Ian Boyd
i am using automation (i.e. COM automation) to display some HTML in Internet Explorer (9):
我正在使用自动化(即 COM 自动化)在 Internet Explorer (9) 中显示一些 HTML:
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:
出现 Internet Explorer,显示我的 html,其开头为:
<!DOCTYPE html>
<HTML>
<HEAD>
...
Note:the html5 standards-mode opt-in doctype
html
注意:html5 标准模式选择加入文档类型
html
Except that the document is not in ie9 standards mode; it's in ie8 standards mode:
除非文档不是ie9标准模式;它处于 ie8 标准模式:
If i save the html to my computer first:
如果我先将 html 保存到我的电脑:
and then view thathtml document, IE is put into standards mode:
然后查看那个html文档,IE进入标准模式:
My question is how update my SpawnIEWithSource(String html)
function to throw the browser into standards mode?
我的问题是如何更新我的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;
}
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:
评论者在 ieblog 条目“使用浏览器模式与文档模式测试站点”上提问:
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?
当 HTML 内容位于嵌入式 web 控件中时,我们能否获得如何确定文档模式的描述?似乎是文档模式的选择不同 - 也许是出于兼容性原因?
MarkSil [MSFT] responded:
MarkSil [MSFT] 回应:
@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.
@Thomas:感谢您提出这个问题。WebBrowser Control 以与 IE 相同的方式确定 doc 模式,因为它包含相同的 Web 平台(例如,在 IE 和 WebBrowser Control 主机之间共享一个 mshtml.dll)。WebBrowser 控件默认为 Compatibility View 浏览器模式,这意味着默认的 doc 模式是 IE7。这是一篇包含更多详细信息的博客文章: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?
@MarcSil (re: WebBrowser Control)
使用注册表项为 WebControl 选择文档模式的问题在于它适用于整个应用程序。我为 Google SketchUp 编写插件,您可以在其中使用 WebDialog 窗口来创建 UI - 它只是窗口中的 WebBrowser 控件。但这会导致问题,因为我想为我的 WebBrowser 控件实例强制使用文档模式,而不是整个 SU 的所有 WebBrowser 控件。
所以,我的问题是:如何控制 WebBrowser 控件的每个实例的文档模式?
回答by Gabriele Petrioli
Have you tried setting in your html the
你有没有试过在你的 html 中设置
<meta http-equiv="X-UA-Compatible" content="IE=9" />
or
或者
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
which means latest version
这意味着最新版本
回答by whitehawk
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:
WebBrowser 控件的 IE9“版本”与 IE8 版本一样,实际上是多个浏览器合二为一。与 IE8 版本不同,您可以通过更改 doctype 对页面内的渲染模式进行更多控制。当然,要更改浏览器模式,您必须像前面的答案一样设置注册表。这是 FEATURE_BROWSER_EMULATION 的位置:
HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)
SOFTWARE
Microsoft
Internet Explorer
Main
FeatureControl
FEATURE_BROWSER_EMULATION
contoso.exe = (DWORD) 000090000
Here is the complete set of codes:
这是完整的代码集:
- 9999 (0x270F)- Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
- 9000 (0x2328)- Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
- 8888 (0x22B8)-Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.
- 8000 (0x1F40)- Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
- 7000 (0x1B58)- Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
- 9999 (0x270F)- Internet Explorer 9。无论 !DOCTYPE 指令如何,网页都以 IE9 标准模式显示。
- 9000 (0x2328)- Internet Explorer 9。包含基于标准的 !DOCTYPE 指令的网页以 IE9 模式显示。
- 8888 (0x22B8) -无论 !DOCTYPE 指令如何,网页都以 IE8 标准模式显示。
- 8000 (0x1F40)- 包含基于标准的 !DOCTYPE 指令的网页以 IE8 模式显示。
- 7000 (0x1B58)- 包含基于标准的 !DOCTYPE 指令的网页以 IE7 标准模式显示。
The full docs:
完整的文档:
http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation
http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation
回答by u109919
FEATURE_BROWSER_EMULATIONdoes not works with CoInternetSetFeatureEnabled. The documentation of INTERNETFEATURELISTis not updated since IE7.
FEATURE_BROWSER_EMULATION不适用于CoInternetSetFeatureEnabled。INTERNETFEATURELIST的文档自 IE7 起未更新。
Since the feature setting is under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl you maybe able to override the value in your process via a registry API hook.
由于功能设置位于 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl 下,您可以通过注册表 API 挂钩覆盖进程中的值。