.Net 修改WebBrowser 內核版本

需求:在winform程序中,加載網頁

做法:在窗口中添加一個panel,再從工具欄中拖入一個WebBroswer瀏覽器控件!

程序中調用webBroswer1.Navigate(URL); 即可加載網頁

 該網站可以檢查瀏覽器的內核版本:  http://gw.hncd.gov.cn/nboa/jsp/system/admin/browser_vali.jsp

但是在加載頁面時有時會出現腳本錯誤!在谷歌瀏覽器上就不會,這是由於IE兼容性不好導致的!

解決方案一:提升內核版本,方法如下:

private static void WebBrowserVersionEmulation()
        {
            const string BROWSER_EMULATION_KEY =
            @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
            //
            // app.exe and app.vshost.exe
            String appname = Process.GetCurrentProcess().ProcessName + ".exe";
            //
            // Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
            const int browserEmulationMode = 11001;

            RegistryKey browserEmulationKey =
                Registry.CurrentUser.OpenSubKey(BROWSER_EMULATION_KEY, RegistryKeyPermissionCheck.ReadWriteSubTree) ??
                Registry.CurrentUser.CreateSubKey(BROWSER_EMULATION_KEY);

            if (browserEmulationKey != null)
            {
                browserEmulationKey.SetValue(appname, browserEmulationMode, RegistryValueKind.DWord);
                browserEmulationKey.Close();
            }
        }

/*  IE各版本的值如下:

        11001 (0x2EDF) Internet Explorer 11. Webpages are displayed in IE11 Standards mode, regardless of the !DOCTYPE directive

        11000 (0x2AF8) :Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode

        10000 (0x2710) :Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.

        10001 (0x2AF7) :Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.

        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.
    */

注:IE瀏覽器的最高版本爲IE 11。目前微軟已經宣佈放棄IE的開發,因此IE 11應該就是最後一個版本,以後應該不會再有更高版本了。

參考文章:

https://blog.csdn.net/cctvcqupt/article/details/47684909

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章