【总结】使用WebBrowser遇到的陷阱

前言

一直想用WebBrowser做一些好玩的东西,比如抓取分析感兴趣的网站页面、自动点击提交页面等,所以最近在研究WebBrowser。WebBrowser的功能十分强大,就是一个微型的Browser,不过它也有自己的一些不足,为了下次避免再次出现这个坑,特在此总结一下。

故障描述

本打算用WebBrowser做一个自助登录的小程序,来登录公司内部的系统(其实公司的登录系统形同虚设,无验证码),本以为万事俱备,只欠东风了,测试的时候才发现,压根儿没给我起作用,只是弹出了脚本错误提示。什么原因呢?我在各个主流浏览器中打开公司的站点,发现并无异常,这下真没辙了。没办法,只能查找相关的资料,结果还真找到了原因。微软开发的WebBrowser运行内核是IE7版本,而现在的主流浏览器都是在IE9或以上,其他的就不说了,于是我抱着试一试的态度把IE调整成IE7兼容模式,果不其然,效果和在WebBrowser中运行效果一模一样,于是我断定,该故障的原因和WebBrowser运行的内核版本有关,找到了问题的原因,自然就知道了解决问题的方向。

排除故障

在微软的网站上找到了一篇文章,关于设置WebBrowser内核运行IE版本的。以下是原文:

Web Browser Control – Specifying the IE Version 
http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

I use the Internet Explorer Web Browser Control in a lot of my applications to display document type layout. HTML happens to be one of the most common document formats and displaying data in this format – even in desktop applications, is often way easier than using normal desktop technologies.

One issue the Web Browser Control has that it’s perpetually stuck in IE 7 rendering mode by default. Even though IE 8 and now 9 have significantly upgraded the IE rendering engine to be more CSS and HTML compliant by default the Web Browser control will have none of it. IE 9 in particular – with its much improved CSS support and basic HTML 5 support is a big improvement and even though the IE control uses some of IE’s internal rendering technology it’s still stuck in the old IE 7 rendering by default.

This applies whether you’re using the Web Browser control in a WPF application, a WinForms app, a FoxPro or VB classic application using the ActiveX control. Behind the scenes all these UI platforms use the COM interfaces and so you’re stuck by those same rules.

Feature Delegation via Registry Hacks
Fortunately starting with Internet Explore 8 and later there’s a fix for this problem via a registry setting. You can specify a registry key to specify which rendering mode and version of IE should be used by that application. These are not global mind you – they have to be enabled for each application individually.

There are two different sets of keys for 32 bit and 64 bit applications.

32 bit:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

64 bit:

HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

The value to set this key to is (taken from MSDN here) as decimal values:

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.

简单翻译一下:

32位和64位系统修改注册表的位置不同

32位:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION

Value Key: contoso.exe = (DWORD) 00000000 注:其中的"contoso.exe"为您的程序名字.即嵌入了WebBrowser控件的可执行程序的名字。

64位:

HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION

Value Key: contoso.exe = (DWORD) 00000000 注:其中的"contoso.exe"为您的程序名字.即嵌入了WebBrowser控件的可执行程序的名字。

另外,我的程序使用Visual Studio 2010编译的时候,如果是Debug模式,那么在注册表中更改的内容无效;如果使用了Release模式,则注册表的内容立即生效!

原文地址:https://www.cnblogs.com/xhb-bky-blog/p/5210505.html