WebBrowser禁用觸摸縮放

最近做一個WPF觸屏的項目,引用到WebBrowser控件,由於是觸屏的所以控件里的網頁可以縮放,客戶提出要求,屏蔽這縮放功能。

於是網上找了很多資料,也換過控件,WebView2 控件使用Microsoft Edge (Chromium)作為呈現引擎。
後來找到一個完美的解決方案,我在這裡直接共享出來。

 

一、設置瀏覽器仿真

  Windows Internet Explorer 8及以後版本。feature_browser_simulation特性定義了Internet Explorer的默認模擬模式,並支持以下值。

11001 (0x2AF9 Internet Explorer 11. Webpages are displayed in IE11 edge mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
11000 (0x2AF8) IE11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode. Default value for IE11.
10001 (0x2711) Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.
10000 (0x02710) Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10.
9999 (0x270F) Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
9000 (0x2328) Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.

Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
 
8888 (0x22B8) Webpages are displayed in IE8 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
8000 (0x1F40) Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8

Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
 
7000 (0x1B58) Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.

1.找到註冊表項:

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

2.添加DWORD(32位)值,重命名為你的程序名,例如QQMusic.exe
3.根據上面對應的數值設置十進制數值;

 

 

二、禁用阻塞本地Script腳本

  Internet Explorer 7及以後版本。在啟用阻塞本地Script腳本特性時,允許存儲在本地機器區域的腳本僅在從本地機器區域加載的網頁上運行。
該屬性默認時啟用的(DWORD) 00000001,我們需將他設為禁用(DWORD) 00000000.

1.找到註冊表項:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BLOCK_LMZ_SCRIPT 

2.添加DWORD(32位)值,並重命名
3.修改十進制數值為0

 

三、禁用傳統輸入模式
  由於微軟在Windows 8以上引入了新的輸入模式,為了使遺留的應用程序提供最廣泛的兼容性,WebBrowser控件在Windows 8以上的系統中會模擬Windows 7鼠標、觸摸和筆的輸入模式。

  傳統輸入模式啟用時:
  1.Trident渲染引擎(mshtml.dll)不處理Windows指針消息。
  2.文檔對象模型(DOM)指針和手勢事件不會觸發。
  3.鼠標和觸摸信息按Windows 7輸入模式發送。
  4.觸摸選擇遵循Windows 7模式(「拖動至選擇」),而不是Windows 8模式(「點擊至選擇」)。
  5.硬件加速平移和縮放被禁用。
  6.Zoom和Pan樣式屬性將會被忽略

第5條和第6條就是導致我不管怎麼去改html樣式,都無法屏蔽縮放的原因。因為傳統輸入模式默認是沒有開啟的,我們只需將他啟用,就可以禁用WebBrowser的觸屏功能了。

1.找到註冊表項:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControlFEATURE_NINPUT_LEGACYMOD

2.添加DWORD(32位)值,並重命名
3.修改十進制數值為0

 

我們只需在WebBrowser初始化之前調用以下方法:

     private void SetBrowSerCompatibilityModel()
        {
            //獲取程序名稱
            var fileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
            if (string.Compare(fileName, "devenv.exe", true) == 0)//確定不是在vs中運行
                return;

            //設置瀏覽器仿真
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                    RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                key.SetValue(fileName, (uint)10000, RegistryValueKind.DWord);
            }

            //禁用阻塞本地Script腳本
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BLOCK_LMZ_SCRIPT",
                    RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                key.SetValue(fileName, (uint)0, RegistryValueKind.DWord);
            }
            //禁用傳統輸入模式
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_NINPUT_LEGACYMOD",
                    RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                key.SetValue(fileName, (uint)0, RegistryValueKind.DWord);
            }
        }