前言
.NET 版本更新了一代又一代,winform中的webbrowser控件的IE内核版本却始终用的IE7,好多网站都对IE7已经不支持。webbrowser这个控件就显得有些鸡肋,经过查找大佬门的代码和资料,终于找到了解决办法。这里做个记录。
代码实现
通用设置类:
using Microsoft.Win32;
using System;
using System.ComponentModel;
namespace Common
{
public class SetWebBrowser
{
/// <summary>
/// 修改注册表信息来兼容当前程序
/// </summary>
/// <param name="ieVersion">设置IE版本</param>
public static void SetWebBrowserFeatures(int ieVersion)
{
//如果在Visual Studio中运行in-proc,不要更改注册表
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_USERSoftwareMicrosoftInternet ExplorerMainFeatureControl";
//设置浏览器对应用程序(appName)以什么模式(ieMode)运行
Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
appName, ieMode, RegistryValueKind.DWord);
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>返回IE版本</returns>
public static int GetBrowserVersion()
{
int browserVersion = 0;
using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftInternet 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; // DOCTYPE指令在IE11标准模式下显示。.
switch (browserVersion)
{
case 7:
mode = 7000; // Internet Explorer 7.
break;
case 8:
mode = 8000; // Internet Explorer 8.
break;
case 9:
mode = 9000; // Internet Explorer 9.
break;
case 10:
mode = 10000; // Internet Explorer 10.
break;
case 11:
mode = 11000; // Internet Explorer 11.
break;
}
return mode;
}
}
}
调用:
public partial class FrmWebBrowser : Form
{
public FrmWebBrowser()
{
InitializeComponent();
//获取浏览器版本
int browserVersion = SetWebBrowser.GetBrowserVersion();
//也可以直接输入调用的版本,例如调用IE11: SetWebBrowser.SetWebBrowserFeatures(11);
//修改注册表兼容当前程序
SetWebBrowser.SetWebBrowserFeatures(browserVersion);
}
}