WebBrowser 设置IE兼容性
生活随笔
收集整理的這篇文章主要介紹了
WebBrowser 设置IE兼容性
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在窗體應(yīng)用程序和wpf程序中會(huì)使用到WebBrowser 來顯示一個(gè)網(wǎng)頁,但是可能要顯示的網(wǎng)頁要求在特定的模式下運(yùn)行,因此需要調(diào)整WebBrowser 對(duì)網(wǎng)頁兼容性的設(shè)置;
通過在注冊(cè)表中設(shè)置HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION中添加一個(gè)值,該值指示該程序以什么模式來運(yùn)行IE
代碼如下(下面代碼是復(fù)制別人,在這里是為了方便查找,有一些看不懂)
?/// <summary>/// 修改注冊(cè)表信息來兼容當(dāng)前程序///?/// </summary>static void SetWebBrowserFeatures(int ieVersion){// don't change the registry if running in-proc inside Visual Studioif (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\";//設(shè)置瀏覽器對(duì)應(yīng)用程序(appName)以什么模式(ieMode)運(yùn)行Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",appName, ieMode, RegistryValueKind.DWord);// enable the features which are "On" for the full Internet Explorer browser//不曉得設(shè)置有什么用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>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);}//如果小于7if (browserVersion < 7){throw new ApplicationException("不支持的瀏覽器版本!");}return browserVersion;}/// <summary>/// 通過版本得到瀏覽器模式的值/// </summary>/// <param name="browserVersion"></param>/// <returns></returns>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 11break;}return mode;}在使用WebBrowser顯示一個(gè)兼容性要求的網(wǎng)頁時(shí),修改SetWebBrowserFeatures方法中后再顯示一個(gè)網(wǎng)頁即可,ieVersion為要兼容的IE的版本
總結(jié)
以上是生活随笔為你收集整理的WebBrowser 设置IE兼容性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim替换命令
- 下一篇: 新手总结必看:想要成为一名合格的软件测试