怎样把WebBrowser控件放到IE9成标准? [英] How to put the WebBrowser control into IE9 into standards?

查看:25
本文介绍了怎样把WebBrowser控件放到IE9成标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动化(即 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 出现,显示我的 html,开头为:

<!DOCTYPE html><HTML><头>...

<块引用>

注意: html5 标准模式选择加入文档类型 html

除非文档不是ie9标准模式;它处于 ie8 标准模式:


如果我先将 html 保存到我的电脑:

然后查看那个 html文档,IE进入标准模式:

我的问题是如何更新我的 SpawnIEWithSource(String html) 函数以使浏览器进入标准模式?

void SpawnIEWithSource(String html){变体 ie = CoInternetExplorer.Create();ie.Navigate2(about:blank");webDocument = ie.Document;webDocument.Write(html);webDocument.Close();ie.Visible = true;}


一个更冗长、更难理解或可读性更强的代码示例,这无助于进一步的问题可能是:

IWebBrowser2 即;CoCreateInstance(CLASS_InternetExplorer, null, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_WebBrowser2, ie);即.AddRef();ie.Navigate2(about:blank");IHtmlDocument 文档;dispDoc = ie.Document;dispDoc.AddRef();dispDoc.QueryInterface(IHTMLDocument2, doc);dispDoc.Release()doc.Write(html);doc.Close();doc.Release();ie.Visible = true;即.Release();


更新

评论者在 ieblog 条目中询问 使用浏览器模式与 Doc 测试网站模式:

<块引用>

当 HTML 内容在嵌入的 web 控件中时,我们能否描述如何确定文档模式?似乎是选择了不同的文档模式 - 可能是出于兼容性原因?

MarkSil [MSFT] 回应:

<块引用>

@Thomas:感谢您提出这个问题.WebBrowser Control 以与 IE 相同的方式确定文档模式,因为它包含相同的 Web 平台(例如,IE 和 WebBrowser Control 主机之间有一个共享的 mshtml.dll).WebBrowser 控件默认为兼容性视图浏览器模式,这意味着默认的文档模式是 IE7.这是一篇博客文章,其中包含更多详细信息:blogs.msdn.com/.../more-ie8-extensibility-improvements.aspx.

Thomas 回应:

<块引用>

@MarcSil(回复:WebBrowser 控件)

使用注册表项为 WebControl 选择文档模式的问题在于它适用于整个应用程序.我为 Google SketchUp 编写插件,您可以在其中使用 WebDialog 窗口来创建 UI——它只是一个窗口中的 WebBrowser 控件.但这会导致问题,因为我想为我的 WebBrowser 控件实例强制使用文档模式,而不是对整个 SU 的所有 WebBrowser 控件.

所以,我的问题是:如何控制 WebBrowser 控件的每个实例的文档模式?

解决方案

你试过在你的html中设置

表示最新版本

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?

解决方案

Have you tried setting in your html the

<meta http-equiv="X-UA-Compatible" content="IE=9" />

or

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

which means latest version

这篇关于怎样把WebBrowser控件放到IE9成标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆