上一篇文章在使用Echarts最新版本时,发现js报错,原因是WebBrowser控件内置的IE版本太低,我们使用下面代码查看内置的IE版本。
private void Form1_Load(object sender, EventArgs e) { if (webBrowser1.Document == null) { //webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "echart\\echarts.html"); showIEVersion() ; } } /// /// 显示ie版本 /// private void showIEVersion() { webBrowser1.DocumentText = " " + " " + " " + " + " " + " " + " + " " + " "; }
运行程序之后如下图所示,我们发现WebBrowser的IE版本是IE7.0
下面来设置程序的IE版本号
1、查看电脑的IE版本号
2、然后通过版本得到浏览器模式的值 ,这里我们可以随便指定IE版本,假设我们的电脑最高支持IE11,但是因为某些原因,我们想要指定我们的html代码在IE10上运行,可以在ie11的判断上返回ie10的模式:
3、指定IE的版本模式之后,我们将该版本号写入注册表
4、此时IE版本指定成功,若想看注册表中的内容,可以在开始菜单内输入"regedit.exe",找到注册表项:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
发现多了一个Echart.vshost.exe程序,对应的IE版本号是IE10。
5、再次查看WebBrowser的IE版本
至此IE版本设置成功,完整代码如下:
private void Form1_Load(object sender, EventArgs e) { //强制使用ie10版本 specifyVer10(); if (webBrowser1.Document == null) { webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "echart\\echarts.html"); } } /// /// 强制使用ie10版本 /// public static void specifyVer10() { try { //获取ie版本 string[] tmpIevers = getIeVer(); if (tmpIevers == null || tmpIevers.Length == 0) { return; } int tmpVer1 = 0; int tmpVer2 = 0; if (tmpIevers.Length == 1) { tmpVer1 = getIEMode(tmpIevers[0]); tmpVer2 = tmpVer1; } if (tmpIevers.Length == 2) { tmpVer1 = getIEMode(tmpIevers[0]); tmpVer2 = getIEMode(tmpIevers[1]); } //取最大版本号 int tmpIeverVal = tmpVer1 > tmpVer2 ? tmpVer1 : tmpVer2; RegistryKey tmpWbverkey = Registry.CurrentUser; updateWebVer(tmpWbverkey, tmpIeverVal); RegistryKey tmpWbverlmkey = Registry.LocalMachine; updateWebVer(tmpWbverlmkey, tmpIeverVal); } catch (Exception) { } } /// /// 获取ie版本 /// /// private static string[] getIeVer() { RegistryKey mreg = Registry.LocalMachine.CreateSubKey("software\\Microsoft\\Internet Explorer"); string IESvcVersion = (String)mreg.GetValue("svcVersion"); string IEVersion = (String)mreg.GetValue("Version"); mreg.Close(); return new string[] { IESvcVersion, IEVersion }; } /// /// 通过版本得到浏览器模式的值 /// /// /// private static int getIEMode(string pVers) { try { int tmpIeverVal = 0; string tmpIever = pVers; if (tmpIever.StartsWith("6.")) { tmpIeverVal = 6666; } if (tmpIever.StartsWith("7.")) { tmpIeverVal = 7000; } if (tmpIever.StartsWith("8.")) { tmpIeverVal = 8000; } if (tmpIever.StartsWith("9.")) { tmpIeverVal = 9000; } if (tmpIever.StartsWith("10.")) { tmpIeverVal = 10000; } if (tmpIever.StartsWith("11.")) { tmpIeverVal = 10000; } return tmpIeverVal; } catch (Exception) { return 9000; } } /// /// 设置程序使用的IE版本 /// /// /// private static void updateWebVer(RegistryKey pRootKey, int pIeVer) { RegistryKey tmpWbverkey = pRootKey; RegistryKey tmpWbverdetailkey = tmpWbverkey.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true); //获取程序名称 string tmpCurProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; //设置浏览器对应用程序(appName)以什么模式(ieMode)运行 if (tmpWbverdetailkey != null) { tmpWbverdetailkey.SetValue(tmpCurProcessName + ".exe", pIeVer, RegistryValueKind.DWord); } RegistryKey tmpWbverdetailkeyfor64 = tmpWbverkey.OpenSubKey("Software\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true); if (tmpWbverdetailkeyfor64 != null) { tmpWbverdetailkeyfor64.SetValue(tmpCurProcessName + ".exe", pIeVer, RegistryValueKind.DWord); } }
IE版本设置成功,下一篇进行JS函数和winform函数之间的相互调用。