1. 程式人生 > >【.Net碼農】WebBrowser與IE的關係,如何設定WebBrowser工作在IE9模式下?

【.Net碼農】WebBrowser與IE的關係,如何設定WebBrowser工作在IE9模式下?

一.問題的提出

偶然發現,Winform裡的WebBrowser和IE實際安裝的版本似乎並不同步,很有趣!

下面有張圖,裡面一個視窗是用IE9開啟某網站,另一個視窗是用Winform+WebBrowser開啟同樣的網站,有意思的事情出現了。

 

在IE9視窗中,這個網站左邊選單樹無法顯示,原因是IE9使用的技術較新,而網站使用的技術較舊,未能及時同步更新到支援IE9所致。該如何辦呢?

微軟在IE9中提供一個相容性檢視功能,只要一點選相容性檢視就可以相容舊版本瀏覽器的網站。
 


引用MSDN “To overcome potential compatibility issues, Internet Explorer supports a feature called Compatibility View that allows users to display troublesome pages in IE7 mode. “ URL: 

http://msdn.microsoft.com/en-us/library/dd567845(v=VS.85).aspx

也就是說,這個相容模式就是IE7的模式。

又查了些ms資料,發現這個相容性檢視在IE8時首次引入,在IE9,IE10中都將繼續支援。

再翻回來看Winform+WebBrowser這個視窗,它開啟同樣的網站卻能顯示左邊選單樹,這樣看來,似乎WebBrowser不用設定就預設自動打開了相容性檢視模式。

現在就去仔細查一下權威資料,核實一下兩個問題:
1.Webbrowser與IE到底是什麼關係?是否確實用ie核心, 是否本質上和360安全瀏覽器,傲遊瀏覽器和騰訊TT等IE核心瀏覽器相同。
2.Webbrowser是否使用相容瀏覽模式,以及這個模式是否能改?

二.查詢結果

1.webbrowser呼叫的就是本機IE9,並且webbrowser預設就是執行在IE7 mode下,除非你改變它.

摘幾句:
Wow first post with such bold claim without any source backing up. You probably should read the IE SDK (the manual you need to read if you want to use the webbrowser control) or dig through the IE programming forums (that's the place others often go when they are stuck on IE programming) if you want to use the webbrowser control.

Webbrowser is a wrapper around IE APIs. There is no such thing as multiple versions of IE coexisting on the same computer. You will always get the one and only version of IE installed on the computer from webbrowser control.

There are many, many documented setting differences between default IE and webbrowser. Basically you don't have to opt out new features in webbrowser that may break your app (the Visual Studio team learned a hard lesson here, when IE8 breaks Visual Studio's wizards) , you have to write code to opt in, unless the improvement is security related. That means the webbrowser will run in IE7 mode unless you change the mode in feature control.

Note some web site declare their requirement of IE7 or IE8 mode. It may not be wise to force the IE9 mode. 

IE must be installed on the machine for you to use Webbrowser Control.

Internet Explorer MUST be installed to use the WebBrowser control.  There are simply no ifs, ands, or buts about it.  How can you expect to use IE functionality if IE is not installed?

3.如何設定WebBrowser在IE9 mode下工作呢?
答曰:需要修改登錄檔,具體看下面4,5,6,尤其6最全面,可以光看6。

摘抄幾句:
How do I make it so the WPF WebBrowser control will use IE9 as the browser engine instead of IE7?
I have some HTML that is rendering differently in the WebBrowser control than in the IE9 browser. When I run the following javascript in the WebBrowser, the result is "7". as in IE7.

I found an article by Rick Strahl that describes registry settings that will get the WebBrowser to use IE9. But I would like to avoid that. And I am interested to know how IE7 comes about being used.http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version
回答:You want to avoid the only documented way to set document compatibility mode for webbrowser hosts? Why?

5.WebBrowser and CSS3 ?
http://social.msdn.microsoft.com/Forums/en-AU/winforms/thread/1b656af7-bda9-47d9-8f9a-1d886d3688ca
Web browser control by default runs in compatibility mode unless you set the feature browser emulation registry key. The fact that IE9 is able to render CSS3 correctly and browser control is not seems to suggest browser control is not running in IE9 standards mode.

You can use 9000 value, unless you want to force IE 9 standards mode for all pages. In case of later, you need to use 9999.

hklm

If hklm and 64bit machine used, you need to check is Wow6432Node needs to be changed.

And finally you need to add process name hosting browser control as value name in the registry key.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION]
"prevhost.exe"=dword:00001f40
"sllauncher.exe"=dword:00001f40
"WindowsFormsApplication1.exe"=dword:0000270f

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_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

64 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_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.

ok, 言盡至此,無話可說了!