C# 視窗程式:如何使用新版的瀏覽器控件?

基礎篇

C# 簡介

開發環境

變數與運算

流程控制

陣列

函數

物件

例外處理

函式庫篇

檔案處理

資料結構

正規表達式

Thread

應用篇

視窗程式

媒體影音

網路程式

遊戲程式

手機程式

資料庫

雲端運算

特殊功能

委派

擴展方法

序列化

LinQ

WPF

網路資源

教學影片

投影片

教學文章

軟體下載

考題解答

101習題

參考:

    var appName = Process.GetCurrentProcess().MainModule.ModuleName;
    Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", appName, 9999, RegistryValueKind.DWord);
    InitializeComponent();

專案下載:NewBrowser.zip

在 C# 中使用 WebBrowser 控制項時,預設使用的是 IE4 (也太老了吧!),並不會自動隨著使用者的更新而改用新的控件,此時您必須更新系統註冊碼,讓 Windows 系統知道你的程式想使用新版的 IE,以下是筆者的參考資料:

It appears you need to fiddle with the registry as per this article: - http://blogs.msdn.com/ie/archive/2009/03/10/more-ie8-extensibility-improvements.aspx

To run a WebBrowser control in IE8 Standards Mode, use the following new value into the registry:

[(HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE)\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] "MyApplication.exe" = dword 8000 (Hex: 0x1F40)

To run in IE7 Standards Mode, use the following registry value:
[(HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE)\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] "MyApplication.exe" = dword 7000 (Hex: 0x1B58)
For IE8 RTM, we’ve added a new “forced” IE8 Standards Mode value. When an application opts into this mode, the Web Browser control will use the IE8 User-Agent string and Browser Emulation mode strictly. It will also ignore fallback features such as the built-in Compatibility View list and the user-generated Compatibility View list when loading pages. To run in “forced” IE8 Standards Mode, use the following registry value:
[(HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE)\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] "MyApplication.exe" = dword 8888 (Hex: 0x22B8)
In all of these examples, “MyApplication.exe” refers to the name of your application.

為了讓您的程式在發布後,都可以強制所有電腦使用新版的 IE,您可以將下列程式放在 Form 一開始啟動的時候,或者是放在 Application 的 Main 程式中,如下所示:

...
        public Form1() A
        {
            InitializeComponent();

            Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\
FeatureControl\FEATURE_BROWSER_EMULATION",
              "Browser.exe",  "dword 8000");
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\
FeatureControl\FEATURE_BROWSER_EMULATION",
              "Browser.vshost.exe", "dword 8000");
        }
...

參考文獻

  1. C# Web Browser component is IE7 not IE8? How to change this?
  2. 如何為 WebBrowser 控制項主機處理指令碼錯誤
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License