首页 > 其他分享 >修改WebBrowser控件的内核解决方案

修改WebBrowser控件的内核解决方案

时间:2022-09-20 01:44:14浏览次数:109  
标签:control 控件 IE8 FEATURE WebBrowser 内核 IE mode

首先说一下原理

当下很大浏览器他们都是用了IE的core, 这个core只提供HTML/JS的执行和渲染,并没有给出关于界面和一些特性上的事,所以开发自己浏览器如果基于IE core需要自己完成这些内容。 一张图很好的说明了这个情况,IE浏览器的架构:http://msdn.microsoft.com/en-us/library/aa741312(VS.85).aspx

ShDocVw 及以下就是WebBrowser的内容,而Browser UI和IE自己的一些特有的功能不属于WebBrowser所有。 当然,不是说要做自己的基于IE的浏览器就非得用WebBrowser, 我们完全可以直接使用 MSHTML 去控制和绘制DOM,跳过WebBrowser。

那么可不可以修改它绑定的内核呢?

这是可以的:

 

IE8 在渲染引擎做了很大的改动,新增加一个标准模式 (Standard Mode)。 不少软件都内嵌了IE的WebBrowser控件(也就是MSHTML.dll)来显示网页, 当用户机器升级到IE8, WebBrowser控件也会随之升级到IE8的渲染引擎。

为了保证这些使用WebBrowser控件的应用软件能够工作起来和原来一样,IE8的WebBrowser控件在默认情况下使用了IE7 的渲染模式(也就是IE8中的Compatible View (兼容视图)模式)。

方法一

加入你想让WebBrowser控件的渲染模式编程IE8的标准模式, 你可以通过设置注册表FEATURE_BROWSER_EMULATION 来实现。

示例:

注册表中注明当前本机装的IE版本
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer
下面有名称为Version的项,其值为IE的版本.
svcVersion =10.0.9200.16618
Version =9.10.9200.16618

[(HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE)\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] 
"MyApplication.exe" = dword 8000 (Hex: 0x1F40)

这里MyApplicaiton.exe 是你的应用程序的EXE文件名。 8000 表示8.0的渲染模式,请对照下表:

IE8 Standards Mode   8000 (0x1F40)  -- IE8 标准模式 (Standard Mode), IE8默认的模式

IE7 Standards Mode   7000 (0x1B58)  -- IE7 兼容视图模式 (Compatible View), IE8的WebBrowser控件默认模式

IE8 Standards Mode (Forced)  8888 (0x22B8) -- IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式

 方法二

 

在html头 加标签 强制使用最新的ie渲染 <meta http-equiv="X-UA-Compatible" content="IE=edge">
强制使用最新的ie8渲染<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>

 

修改案例:

void WINAPI WriteWebBrowserRegKey(LPCTSTR lpKey,DWORD dwValue)
{
    HKEY hk;
    CString str = "Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\";
    str += lpKey;
    if (RegCreateKey(HKEY_LOCAL_MACHINE,str,&hk)!=0)
    {
        MessageBox(NULL,"打开注册表失败!","Error",0);
        ExitProcess(-1);
    }
    if (RegSetValueEx(hk,"你的exe名称.exe",NULL,REG_DWORD,(const byte*)&dwValue,4)!=0)
    {
        RegCloseKey(hk);
        MessageBox(NULL,"写注册表失败!","Error",0);
        ExitProcess(-1);
    }
    RegCloseKey(hk);
}

 

WriteWebBrowserRegKey("FEATURE_BROWSER_EMULATION",9000);
    //    WriteWebBrowserRegKey("FEATURE_ACTIVEX_REPURPOSEDETECTION",1);
        WriteWebBrowserRegKey("FEATURE_BLOCK_LMZ_IMG",1);
        WriteWebBrowserRegKey("FEATURE_BLOCK_LMZ_OBJECT",1);
        WriteWebBrowserRegKey("FEATURE_BLOCK_LMZ_SCRIPT",1);
        WriteWebBrowserRegKey("FEATURE_Cross_Domain_Redirect_Mitigation",1);
        WriteWebBrowserRegKey("FEATURE_ENABLE_SCRIPT_PASTE_URLACTION_IF_PROMPT",1);
        WriteWebBrowserRegKey("FEATURE_LOCALMACHINE_LOCKDOWN",1);
        WriteWebBrowserRegKey("FEATURE_GPU_RENDERING",1);

 

 

参考资料

现在就去仔细查一下权威资料,核实一下两个问题: 
1.Webbrowser与IE到底是什么关系?是否确实用ie内核, 是否本质上和360安全浏览器,傲游浏览器和腾讯TT等IE内核浏览器相同。 
2.Webbrowser是否使用兼容浏览模式,以及这个模式是否能改?

二.查询结果

1.webbrowser调用的就是本机IE9,并且webbrowser默认就是运行在IE7 mode下,除非你改变它.

发现一个msdn的帖子,明确表示webbrowser调用的就是本机IE9,并且webbrowser默认就是运行在IE7 mode下,除非你改变它。 
How to make c# WebBrowser equivalent to IE browser  
http://social.msdn.microsoft.com/Forums/en/winforms/thread/2ed65b9d-c601-4ca8-bde1-64584fc87515

摘几句: 
Wow first post with such bold claim without any source backing up. You probably should read the IE SDK (the manual you need to read if you want to use the webbrowser control) or dig through the IE programming forums (that's the place others often go when they are stuck on IE programming) if you want to use the webbrowser control.

Webbrowser is a wrapper around IE APIs. There is no such thing as multiple versions of IE coexisting on the same computer. You will always get the one and only version of IE installed on the computer from webbrowser control.

There are many, many documented setting differences between default IE and webbrowser. Basically you don't have to opt out new features in webbrowser that may break your app (the Visual Studio team learned a hard lesson here, when IE8 breaks Visual Studio's wizards) , you have to write code to opt in, unless the improvement is security related. That means the webbrowser will run in IE7 mode unless you change the mode in feature control.

Note some web site declare their requirement of IE7 or IE8 mode. It may not be wise to force the IE9 mode. 

2.微软新闻组的一个帖子,Webbrowser Control without IE,里面明确提到,不装IE,无法用webbrowser. 
http://groups.google.com/group/microsoft.public.vb.controls/browse_thread/thread/7575bd25e0730ded/aa40f3dfc799407d?lnk=gst&q=WebBrowser+ie#aa40f3dfc799407d

IE must be installed on the machine for you to use Webbrowser Control.

Internet Explorer MUST be installed to use the WebBrowser control.  There are simply no ifs, ands, or buts about it.  How can you expect to use IE functionality if IE is not installed?

3.如何设置WebBrowser在IE9 mode下工作呢? 
答曰:需要修改注册表,具体看下面4,5,6,尤其6最全面,可以光看6。

4.WPF webbrowser control using IE7 instead of IE9 
http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/4431908e-1869-4435-bcea-a3ec0820edfb

摘抄几句: 
How do I make it so the WPF WebBrowser control will use IE9 as the browser engine instead of IE7? 
I have some HTML that is rendering differently in the WebBrowser control than in the IE9 browser. When I run the following javascript in the WebBrowser, the result is "7". as in IE7.

I found an article by Rick Strahl that describes registry settings that will get the WebBrowser to use IE9. But I would like to avoid that. And I am interested to know how IE7 comes about being used.http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version 
回答:You want to avoid the only documented way to set document compatibility mode for webbrowser hosts? Why?

5.WebBrowser and CSS3 ? 
http://social.msdn.microsoft.com/Forums/en-AU/winforms/thread/1b656af7-bda9-47d9-8f9a-1d886d3688ca 
Web browser control by default runs in compatibility mode unless you set the feature browser emulation registry key. The fact that IE9 is able to render CSS3 correctly and browser control is not seems to suggest browser control is not running in IE9 standards mode.

You'll need to set Browser emulation feature key (FEATURE_BROWSER_EMULATION) described at this link http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx

You can use 9000 value, unless you want to force IE 9 standards mode for all pages. In case of later, you need to use 9999.

hklm

If hklm and 64bit machine used, you need to check is Wow6432Node needs to be changed.

And finally you need to add process name hosting browser control as value name in the registry key.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION] 
"prevhost.exe"=dword:00001f40 
"sllauncher.exe"=dword:00001f40 
"WindowsFormsApplication1.exe"=dword:0000270f

6.Web Browser Control – Specifying the IE Version 
http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

I use the Internet Explorer Web Browser Control in a lot of my applications to display document type layout. HTML happens to be one of the most common document formats and displaying data in this format – even in desktop applications, is often way easier than using normal desktop technologies.

One issue the Web Browser Control has that it’s perpetually stuck in IE 7 rendering mode by default. Even though IE 8 and now 9 have significantly upgraded the IE rendering engine to be more CSS and HTML compliant by default the Web Browser control will have none of it. IE 9 in particular – with its much improved CSS support and basic HTML 5 support is a big improvement and even though the IE control uses some of IE’s internal rendering technology it’s still stuck in the old IE 7 rendering by default.

This applies whether you’re using the Web Browser control in a WPF application, a WinForms app, a FoxPro or VB classic application using the ActiveX control. Behind the scenes all these UI platforms use the COM interfaces and so you’re stuck by those same rules.

Feature Delegation via Registry Hacks 
Fortunately starting with Internet Explore 8 and later there’s a fix for this problem via a registry setting. You can specify a registry key to specify which rendering mode and version of IE should be used by that application. These are not global mind you – they have to be enabled for each application individually.

There are two different sets of keys for 32 bit and 64 bit applications.

32 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

64 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

The value to set this key to is (taken from MSDN here) as decimal values:

9999 (0x270F) 
Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

9000 (0x2328) 
Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.

8888 (0x22B8) 
Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.

8000 (0x1F40) 
Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.

7000 (0x1B58) 
Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.

标签:control,控件,IE8,FEATURE,WebBrowser,内核,IE,mode
From: https://www.cnblogs.com/tang-delphi/p/16709711.html

相关文章

  • Delphi WebBrowser控件的使用中出现的bug
     1、WebBrowser.Visible=false;Visible属性不能使WebBrowser控件不可见,暂时用 WebBrowser.Hide的方法代替,WebBrowser.Hide隐藏浏览器, WebBrowser.Show显示浏览器;   ......
  • Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决
    最近用EmbeddedWB控件做浏览器相关应用的时候,发现有些网页只要一打开就一定会蹦出一个“Invalidfloatingpointoperation”异常(关掉异常对话框以后,浏览器无响应),而程序仅......
  • Delphi中TWebBrowser中注入Js
    最近帮朋友做一个软件,其中要自动化某网页中的操作,最简的操作是调用自己写的代码。代码如下:procedureTForm1.Button2Click(Sender:TObject);var i:integer; h:IHTML......
  • Delphi WebBrowser控件
    WebBrowser控件属性:1、Application如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDispatch)。如果在宿主对象中自动化对象无效,程序将返回WebB......
  • ASR6500S SIP模块与SX1262系列集成替代SX1278 SX1262内核+RF前端
    ASR6500S是一系列LoRaSIP模块,集成了RF前端和LoRa无线电收发器SX1262系列,支持LoRa和FSK调制。LoRa技术是一种针对LPWAN应用的低数据速率、超远程、超低功耗通信进行优化的......
  • Windows内核逆向【目录】
    基础汇编C语言C++Win32PE硬编码内核保护模式驱动系统调用进程线程句柄表APC回调DPC同步内存X64121314驱动读写获取模块win7通信win10通信......
  • TEditWebBrowser 组件
    继承TWebBrowser组件编写的组件:TEditWebBrowser,改写了原Mouse、Keyboard事件和OnEnter、OnExit事件。改写了TWebBrowser的焦点设置获取方法。超强的Edit功能,完全替代TRichE......
  • delphi中WEBBrowser网页html相互调用(一)
    1、基本操作1.1、激活vardoc,url:Olevariant;beginurl:='about:blank';//或者一个有实际意义的urlWebBrowser1.Navigate2(url);//这样就激活了!end;1.2、写HTM......
  • 使用 Buffered Paint API 绘制带有淡入淡出动画的控件
    使用BufferedPaintAPI绘制带有淡入淡出动画的控件发表于2011年10月23日 Windows窗体提供了许多机制来构建与操作系统风格相匹配的专业自定义UI控件;通过结......
  • 重新审视下拉控件
    发表于2012年3月21日不时返回并重新检查旧代码是个好主意。随着时间的推移,会有更多的知识和新的技巧;在我早期的两个自定义Windows窗体控件的情况下,这当然是正确的......