WebBrowser内核指定

原贴在此:http://www.cnblogs.com/doscanner/p/5611434.html

WebBrowser内核指定

一、背景

这几天在维护公司的一个项目,嗯…到现在七八年没人动过了(也是老古董了),都说N年前的代码碰不得 处处是坑 不能挖坑还得一步一步的填坑,恰好今天就填了一坑 此处作为记录 供以后翻阅,对代码除了有些看不懂或者说是很凌乱之外,其他都还行(没注释、有注释的地方是自动生成的英文注释…..、包含 各种委托、事件、多线程、用户控件等等)。

二、问题

对 就是一个CS程序,其中有个功能是输入网址然后展示网页内容,so问题来了 很多网站不支持IE8以下内核的浏览器(很粗暴的跳转到浏览器下载页面~~)。那为什么不用webkit内核的呢…刚刚也说了这是七八年前的代码了…,用的是WebBrowser WebBrowser WebBrowser,说给换一个吧 时间又不允许 而且…你懂的~~(主要是调整太多),也就只能死马当活马医了。

三、解决方式

1.升级IE浏览器

第一时间 想到的就是升级IE浏览器,说干就干 下载安装最新版IE浏览器…重启…运行程序…输入网址… 很粗暴的跳转到浏览器下载页面~~,看来还是太天真… 原来WebBrowser默认使用IE7内核、只升级浏览器是没用的,需要手动指定

2.手动指定WebBrowser内核

浏览了许多资料后,找到了解决方案:改注册表

在开始菜单内输入“regedit.exe”,进入注册表编辑器

找到注册表项:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

在右侧空白区域内单击鼠标右键,点击[新建]→[DWORD(32-位)值]

image

新建的项取名为MyApplication.exe,编辑值时,选择基数“十进制”,填写数值数据,这里填写11000(IE11)

image

最后运行MyAplication.exe,发现问题已经解决

不同IE版本所对应的DWORD值:

image

原文:https://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx#browser_emulation

但是….这样手动去改注册表始终比较麻烦,本着能用代码解决的问题就尽量用代码来解决的想法,决定改改

3.自动指定WebBrowser内核

思路:检测IE版本、根据不同IE版本返回DWORD值、修改注册表

直接贴代码吧,里面有注释

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
public  class  IEVersion
   {
       /// <summary>
       /// IE WebBrowser内核设置
       /// </summary>
       public  static  void  BrowserEmulationSet()
       {
           //当前程序名称
           var  exeName = Process.GetCurrentProcess().ProcessName +  ".exe" ;
           //系统注册表信息
           var  mreg = Registry.LocalMachine;
           //IE注册表信息
           var  ie = mreg.OpenSubKey( @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION" , RegistryKeyPermissionCheck.ReadWriteSubTree);
           if  (ie !=  null )
           {
               try
               {
                   var  val = ieVersionEmulation(ieVersion());
                   if  (val != 0)
                   {
                       ie.SetValue(exeName, val);
                   }
                   mreg.Close();
               }
               catch  (Exception ex)
               {
                   Console.Write(ex.Message);
               }
           }
       }
 
       /// <summary>
       /// IE版本号
       /// </summary>
       /// <returns></returns>
       static  int  ieVersion()
       {
           //IE版本号
           RegistryKey mreg = Registry.LocalMachine;
           mreg = mreg.CreateSubKey( "SOFTWARE\\Microsoft\\Internet Explorer" );
 
           //更新版本
           var  svcVersion = mreg.GetValue( "svcVersion" );
           if  (svcVersion !=  null )
           {
               mreg.Close();
               var  v = svcVersion.ToString().Split( '.' )[0];
               return  int .Parse(v);
           }
           else
           {
               //默认版本
               var  ieVersion = mreg.GetValue( "Version" );
               mreg.Close();
               if  (ieVersion !=  null )
               {
                   var  v = ieVersion.ToString().Split( '.' )[0];
                   return  int .Parse(v);
               }
           }
           return  0;
       }
 
       /// <summary>
       /// 根据IE版本号 返回Emulation值
       /// </summary>
       /// <param name="ieVersion"></param>
       /// <returns></returns>
       static  int  ieVersionEmulation( int  ieVersion)
       {
           //IE7 7000 (0x1B58)
           if  (ieVersion < 8)
           {
               return  0;
           }
           if  (ieVersion == 8)
           {
               return  0x1F40; //8000 (0x1F40)、8888 (0x22B8)
           }
           if  (ieVersion == 9)
           {
               return  0x2328; //9000 (0x2328)、9999 (0x270F)
           }
           else  if  (ieVersion == 10)
           {
               return  0x02710; //10000 (0x02710)、10001 (0x2711)
           }
           else  if  (ieVersion == 11)
           {
               return  0x2AF8; //11000 (0x2AF8)、11001 (0x2AF9
           }
           return  0;
       }
   }

最后,在程序启动时调用:

IEVersion.BrowserEmulationSet();

然后检查注册表,发现已经修改成功。再次运行程序,发现问题已经解决。

 

以上只是本人解决问题的一个方式方法,不官方不权威,不当之处烦请指正

花了一天时间,解决了一个问题(只是暂时的),想想还是挺悲哀的,这工作效率要是被老板知道了…

这是一个很多年前的代码,即使他很古董,但其中必然有我值得学习的地方,这只是第一步,后面还有许多坑要去填,几年下来也确实累积了许多bug,至少曾经还是辉煌过…

当然,会改版的(正决定往Web版调整呢)….

 

回头看了看自己的博客,惨(zhi)不(guai)忍(tai)睹(lan),后面还是得多添砖加瓦了

Chrome based Browser Engine for .NET EO.WebBrowser is a web browser engine based on Google's Chromium project but with native .NET programming interface --- don't worry, it's not a wrapper around the Chrome browser installed on your machine. In fact EO.WebBrowser has the whole browser engine embedded inside a single .NET DLL. In another word, it has zero external dependency. Just reference and use. Why use EO.WebBrowser? • Based on Google's Chrome Project EO.WebBrowser uses the same core Google's Chrome and Apple Safari uses. It does not rely on IE. The engine is much faster and safer. • Zero External Dependency What if user updates/uninstall their browser? What if user disables JavaScript in Internet Explorer's settings dialog? These questions does not exist with EO.WebBrowser because everything is embedded inside our DLL files. • Native .NET components written in C# Because it's written in .NET, you can use it with any .NET based language/development tool. The same DLL works for both 32 bit and 64 bit environments; • Easy to use Programming Interface EO.WebBrowser offers core components that can be used in any Windows application, as well as wrapper controls for both Windows Forms applications and WPF applications; • Extensive Customization Options EO.WebBrowser offers extensive customization options that allow you to customize context menu, hot keys, JavaScript dialogs, file dialogs, focus and window control. Together these features allow you to seamlessly integrate the browser engine into your application; • .NET code -> JavaScript code Turn any web page into an integral part of your application -- both visually and programmatically. You can execute JavaScript code and access all the JavaScript objects directly from your .NET code. Access their properties or even call a JavaScript function are all different options available to you; • JavaScript code -> .NET code Things always go both ways --- and this is reflected in our programming interface as well. You can call JavaScript code from .NET code, and the other way around is also true --- you can call .NET code from your JavaScript code. This allows your Web page to seamlessly interact with the host application; • Custom Resource Handler Want to keep an eye on everything? Or want to keep everything to yourself? We got you covered. EO.WebBrowser offers ability to intercept and modify all requests that originate from the browser engine. For example, you can automatically deny all request sent to a specific host. It also offers you the ability to implement custom protocols or custom resource handlers. For example, you can implement a custom request handler to load images from your database instead of a Web server;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值