1. 程式人生 > >【.Net碼農】修改WebBrowser控制元件的核心解決方案

【.Net碼農】修改WebBrowser控制元件的核心解決方案

首先說一下原理

當下很大瀏覽器他們都是用了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下,除非你改變它.

摘幾句: 
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. 

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。

摘抄幾句: 
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 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

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.

相關推薦

.Net修改WebBrowser控制元件核心解決方案

首先說一下原理 當下很大瀏覽器他們都是用了IE的core, 這個core只提供HTML/JS的執行和渲染,並沒有給出關於介面和一些特性上的事,所以開發自己瀏覽器如果基於IE core需要自己完成這些內容。 一張圖很好的說明了這個情況,IE瀏覽器的架構:http://msdn.microsoft.com/e

.NetWebBrowser與IE的關係,如何設定WebBrowser工作在IE9模式下?

一.問題的提出 偶然發現,Winform裡的WebBrowser和IE實際安裝的版本似乎並不同步,很有趣! 下面有張圖,裡面一個視窗是用IE9開啟某網站,另一個視窗是用Winform+WebBrowser開啟同樣的網站,有意思的事情出現了。   在IE9視窗中,這個網站左邊選單樹無法顯示,原因是IE9使用

.Net在 Web 窗體上報表檢視器 Web 控制元件需要 System.Web.UI.ScriptManager

異常詳細資訊: Microsoft.Reporting.WebForms.ScriptManagerNotFoundException: 在 Web 窗體上報表檢視器 Web 控制元件需要 System.Web.UI.ScriptManager。 源錯誤: [沒有相

.NetC#中的partial class(部分類)

C# 2.0 可以將類、結構或介面的定義拆分到兩個或多個原始檔中,在類宣告前新增partial關鍵字即可。 例如:下面的PartialTest類 class PartialTest { string Str_FieldTest; int Int_FieldTes

.NetC#.net檔案批量上傳解決方案附下載(swfupload)2015-8-28更新

因為最近專案需要多檔案同時上傳所以自己在網上找了下方法。swfupload做到了,所以我把我的C#.net環境的多檔案同時上傳共享給大家!(本例項最大隻能上傳500M的資料,如需要上傳更大的下面會告訴大家如何設定) 功能完全支援ie和firefox瀏覽器! 一般的WEB方式檔案上傳只能一個一個的進行上傳

.NetC# 啟動EXE檔案及帶啟動引數EXE

(一)、先製作一個帶啟動引數的EXE檔案。  步驟:             1、定義全域性私有變數:private string[] s = new string[1];  //這裡為了簡單起見,只做一個引數            2、  在窗體的建構

.NetABP學習目錄傳送門

https://aspnetboilerplate.com/Pages/Documents/Zero/Startup-Template入門教程http://www.cnblogs.com/mienreal/p/4528470.htmlhttps://www.cnblogs.c

.NetWPF介面設計—擼大師

WPF介面設計,模仿了金山衛士,360,魯大師的介面! <!--無邊框窗體-->     <Stylex:Key="NoResize_window"TargetType="{x:Type Window}">     <Set

.NetDataGrid 資料繫結使用小結一

  做過ASP.NET開發的朋友都知道,ASP.NET提供了強大的列表資料控制元件,從ASP.NET1.1 時代的DataGrid,到ASP.NET2.0時代的GridView,再到ASP.NET3.5時代的ListView,功能越 來越強大,使用起來也非常的靈活.在Silverlight2中,同樣提供了一

.NetC#反射獲取 所有欄位 及 私有欄位

using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace ReflectionDemo {

.NetNPOINPOI對Excel的操作(Sheet轉DataTable、List)

通過NPOI對Excel進行操作,這裡主要是讀取的操作。封裝到ExcelHelper操作類中。 using System.Collections.Generic; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.U

.Net初試T4模板

工具下載地址:【二選一 個人喜歡用 devart T4 Editor for Visual Studio 小巧 有些朋友 從網上下載外掛後 開啟專案 還是顯示純黑字型 沒有著色 和提示功能,但新建專案 和 新建模板 又正常解答【把你的老專案 升級上來

.Net用asp.net實現遠端獲取其他網站頁面指定內容

遠端獲取網頁內容.經過一定的處理和靈活應用,可以開發成成體系網站內容採集系統.通常也叫做"新聞小偷"一般來說.做內容採集分為如下幾個大致的步驟:   1.遠端獲取頁面的全部Html源文字.   2.通過過濾處理,分析有效內容文字.(通常用正則表示式來擷取有效資料)   3.將格式有效的資料,根據自己的資料庫結

.Net.NET中執行js指令碼的方法

一、後臺註冊js指令碼 在專案開發中,遇到了問題,當使用了UpdatePanel控制元件後,直接在後臺輸出js指令碼報錯了。 大家都知道向客戶端輸出內容的方式很多,而大多數初學者會使用Respone.Write(string)。比如: 以下是程式碼片段:

.NetMVC多層架構+MVC+EF+AUTOFAC+AUTOMAPPER

1 namespace YTJWGL_EFDao 2 { 3 public class BaseEFDao<T> : IBaseDao<T> where T : class,new()//限制T的型別為class或者物件 4 { 5 6

.Net認識ASP.NET MVC的5種AuthorizationFilter

在總體介紹了篩選器及其提供機制(《深入探討ASP.NET MVC的篩選器》)之後,我們按照執行的先後順序對四種不同的篩選器進行單獨介紹,首先來介紹最先執行的AuthorizationFilter。從命名來看,AuthorizationFilter用於完成授權相關的工作,所以它應該在Action方法被呼叫之前執

修改WebBrowser控制元件核心解決方案

首先說一下原理 當下很大瀏覽器他們都是用了IE的core, 這個core只提供HTML/JS的執行和渲染,並沒有給出關於介面和一些特性上的事,所以開發自己瀏覽器如果基於IE core需要自己完成這些內容。 一張圖很好的說明了這個情況,IE瀏覽器的架構:http://msdn.microsoft.com/

PyQt5-Qt Designer工具箱(QToolBox)控制元件的使用

工具箱(QToolBox)+toolButton+tabWidget 總體介紹 QToolBox類提供了一列選項卡的小部件(選項卡內含專案)。   工具箱是一個小部件,它將選項卡一個一個的顯示,當前專案顯示在當前選項卡下方。每個選項卡在選項卡列中都有一個索引位置。一個選項卡的專案是一個QWi

修改WebBrowser控制元件核心解決方案(x86和x64有不同)

首先說一下原理 當下很大瀏覽器他們都是用了IE的core, 這個core只提供HTML/JS的執行和渲染,並沒有給出關於介面和一些特性上的事,所以開發自己瀏覽器如果基於IE core需要自己完成這些內容。 一張圖很好的說明了這個情況,IE瀏覽器的架構:http://ms

React Native開發React Native控制元件之Image元件講解與美團首頁頂部效果例項(10)

轉載請標明出處:(一)前言        【好訊息】個人網站已經上線執行,後面部落格以及技術乾貨等精彩文章會同步更新,請大家關注收藏:http://www.lcode.org      今天我們一起來看一下Image元件的相關使用講解以及模仿實現一下美團首頁頂部分類的效果。具體環境搭建以及相關配置的請檢視之前