如何把IE9的WebBrowser控件纳入标准?

发布于 2024-09-30 11:31:01 字数 2587 浏览 5 评论 0原文

我正在使用自动化(即 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 标准模式: alt text


如果我先将 html 保存到我的计算机上:

alt text

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

alt text 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:
alt text


If i save the html to my computer first:

alt text

and then view that html document, IE is put into standards mode:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-10-07 11:31:01

您是否尝试过在 html 中设置

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

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

表示最新版本

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

似狗非友 2024-10-07 11:31:01

IE9“版本”的WebBrowser控件与IE8版本一样,实际上是多个浏览器合二为一。与 IE8 版本不同,您可以通过更改文档类型对页面内的渲染模式进行更多控制。当然,要更改浏览器模式,您必须像之前的答案一样设置注册表。这是 FEATURE_BROWSER_EMULATION 的位置:

HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)
     SOFTWARE
          Microsoft
               Internet Explorer
                    Main
                         FeatureControl
                              FEATURE_BROWSER_EMULATION
                                   contoso.exe = (DWORD) 000090000

这是完整的代码集:

  • 9999 (0x270F) - Internet Explorer 9。
    网页在IE9中显示
    标准模式,无论
    !DOCTYPE 指令。
  • 9000 (0x2328) - Internet Explorer 9。包含基于标准的 !DOCTYPE 的网页
    指令在 IE9 模式下显示。
  • 8888 (0x22B8) - 网页是
    在 IE8 标准模式下显示,
    无论 !DOCTYPE 指令如何。
  • 8000 (0x1F40) - 包含以下内容的网页
    基于标准的 !DOCTYPE 指令
    在IE8模式下显示。
  • 7000 (0x1B58) - 包含以下内容的网页
    基于标准的 !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:

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.

The full docs:

http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation

情栀口红 2024-10-07 11:31:01

FEATURE_BROWSER_EMULATION 不适用于 CoInternetSetFeatureEnabledINTERNETFEATURELIST 的文档未更新从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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文