1. 程式人生 > 其它 >Winform設定WebBowser訪問網頁核心版本

Winform設定WebBowser訪問網頁核心版本

封裝了一個通用類:

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helper
{
    public class WebBrowserHelper
    {
        /// <summary>  
        /// 修改登錄檔資訊來相容當前程式
        /// </summary>
public static void SetWebBrowserFeatures(int ieVersion) { // don't change the registry if running in-proc inside Visual Studio if (LicenseManager.UsageMode != LicenseUsageMode.Runtime) return; //獲取程式及名稱 var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
//得到瀏覽器的模式的值 UInt32 ieMode = GeoEmulationModee(ieVersion); var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\"; //設定瀏覽器對應用程式(appName)以什麼模式(ieMode)執行 Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION
", appName, ieMode, RegistryValueKind.DWord); // enable the features which are "On" for the full Internet Explorer browser //不曉得設定有什麼用 Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", appName, 1, RegistryValueKind.DWord); //Registry.SetValue(featureControlRegKey + "FEATURE_AJAX_CONNECTIONEVENTS", // appName, 1, RegistryValueKind.DWord); //Registry.SetValue(featureControlRegKey + "FEATURE_GPU_RENDERING", // appName, 1, RegistryValueKind.DWord); //Registry.SetValue(featureControlRegKey + "FEATURE_WEBOC_DOCUMENT_ZOOM", // appName, 1, RegistryValueKind.DWord); //Registry.SetValue(featureControlRegKey + "FEATURE_NINPUT_LEGACYMODE", // appName, 0, RegistryValueKind.DWord); } /// <summary> /// 獲取瀏覽器的版本 /// </summary> /// <returns></returns> public static int GetBrowserVersion() { int browserVersion = 0; using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer", RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.QueryValues)) { var version = ieKey.GetValue("svcVersion"); if (null == version) { version = ieKey.GetValue("Version"); if (null == version) throw new ApplicationException("Microsoft Internet Explorer is required!"); } int.TryParse(version.ToString().Split('.')[0], out browserVersion); } //如果小於7 if (browserVersion < 7) { throw new ApplicationException("不支援的瀏覽器版本!"); } return browserVersion; } /// <summary> /// 通過版本得到瀏覽器模式的值 /// </summary> /// <param name="browserVersion"></param> /// <returns></returns> public static UInt32 GeoEmulationModee(int browserVersion) { UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. switch (browserVersion) { case 7: mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. break; case 8: mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. break; case 9: mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. break; case 10: mode = 10000; // Internet Explorer 10. break; case 11: mode = 11000; // Internet Explorer 11 break; } return mode; } } }
View Code
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helper
{
    public class WebBrowserHelper
    {
        /// <summary>  
        /// 修改登錄檔資訊來相容當前程式
        /// </summary>  
        public static void SetWebBrowserFeatures(int ieVersion)
        {
            // don't change the registry if running in-proc inside Visual Studio  
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;
            //獲取程式及名稱  
            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            //得到瀏覽器的模式的值  
            UInt32 ieMode = GeoEmulationModee(ieVersion);
            var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";
            //設定瀏覽器對應用程式(appName)以什麼模式(ieMode)執行  
            Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
                appName, ieMode, RegistryValueKind.DWord);
            // enable the features which are "On" for the full Internet Explorer browser  
            //不曉得設定有什麼用  
            Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
                appName, 1, RegistryValueKind.DWord);


            //Registry.SetValue(featureControlRegKey + "FEATURE_AJAX_CONNECTIONEVENTS",  
            //    appName, 1, RegistryValueKind.DWord);  


            //Registry.SetValue(featureControlRegKey + "FEATURE_GPU_RENDERING",  
            //    appName, 1, RegistryValueKind.DWord);  


            //Registry.SetValue(featureControlRegKey + "FEATURE_WEBOC_DOCUMENT_ZOOM",  
            //    appName, 1, RegistryValueKind.DWord);  


            //Registry.SetValue(featureControlRegKey + "FEATURE_NINPUT_LEGACYMODE",  
            //    appName, 0, RegistryValueKind.DWord);  
        }
        /// <summary>  
        /// 獲取瀏覽器的版本  
        /// </summary>  
        /// <returns></returns>  
        public static int GetBrowserVersion()
        {
            int browserVersion = 0;
            using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
                RegistryKeyPermissionCheck.ReadSubTree,
                System.Security.AccessControl.RegistryRights.QueryValues))
            {
                var version = ieKey.GetValue("svcVersion");
                if (null == version)
                {
                    version = ieKey.GetValue("Version");
                    if (null == version)
                        throw new ApplicationException("Microsoft Internet Explorer is required!");
                }
                int.TryParse(version.ToString().Split('.')[0], out browserVersion);
            }
            //如果小於7  
            if (browserVersion < 7)
            {
                throw new ApplicationException("不支援的瀏覽器版本!");
            }
            return browserVersion;
        }
        /// <summary>  
        /// 通過版本得到瀏覽器模式的值  
        /// </summary>  
        /// <param name="browserVersion"></param>  
        /// <returns></returns>  
        public static UInt32 GeoEmulationModee(int browserVersion)
        {
            UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.   
            switch (browserVersion)
            {
                case 7:
                    mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.   
                    break;
                case 8:
                    mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.   
                    break;
                case 9:
                    mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.                      
                    break;
                case 10:
                    mode = 10000; // Internet Explorer 10.  
                    break;
                case 11:
                    mode = 11000; // Internet Explorer 11  
                    break;
            }
            return mode;
        }
    }
}

呼叫方法:

// webBrowser樣式
this.webBrowser1.Location = new Point(0, 60);
this.webBrowser1.Size = new Size(1080, 503);
WebBrowserHelper.SetWebBrowserFeatures(11);//核心設為ie11
this.webBrowser1.Navigate(ConfigurationManager.AppSettings["DomainName"] + "/Show/Notice/Banner");