Web浏览器控制仿真问题(FEATURE_BROWSER_EMULATION) [英] Web browser control emulation issue (FEATURE_BROWSER_EMULATION)

查看:4338
本文介绍了Web浏览器控制仿真问题(FEATURE_BROWSER_EMULATION)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS2013我创建了一个非常简单的Web浏览器控制应用程序,导航到 http:// demos.dojotoolkit.org/demos/calendar/demo.html

Using VS2013 I created a very simple web browser Control application that navigates to http://demos.dojotoolkit.org/demos/calendar/demo.html

如果在注册表中未对此应用程序设置FEATURE_BROWSER_EMULATION,则网站功能正常,添加应用程序到此注册表项(在HKLM下)它正在工作,直到IE9模拟,但失败与IE10和IE11值(我有我的机器上的IE11)。

When no FEATURE_BROWSER_EMULATION is set for this application in the registry the site functions correctly, When adding the application to this registry key (under HKLM) it is working till IE9 emulation but fails with IE10 and IE11 values (i have IE11 on my machine).

示例: / p>

example:

FEATURE_BROWSER_EMULATION

myApp=9999 - works

myApp=10001 - doesn't work

不工作=月日期选择器不工作
任何建议可能是问题?

doesn't work = month date picker is not working Any suggestion what can be the problem?

感谢,
Guy

Thanks, Guy

推荐答案

p>下面是我的 WebBrowser playground应用程序(在C#中),它与您的URL(http://demos.dojotoolkit.org/demos/calendar/demo.html )。

Below is my WebBrowser playground application (in C#) which works well with your URL (http://demos.dojotoolkit.org/demos/calendar/demo.html).

停用 FEATURE_NINPUT_LEGACY_MODE 是什么导致了区别,我相信。还有一些我启用的其他设置,以及。它还显示了如何使用 HKEY_CURRENT_USER 而不是 HKLM ,因此应用程序不需要管理员权限。

Disabling FEATURE_NINPUT_LEGACY_MODE is what made the difference, I believe. There is a couple of other settings I enabled, as well. It also shows how to use HKEY_CURRENT_USER instead of HKLM, so the app doesn't require admin rights.

using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebBrowserApp
{
    public partial class MainForm : Form
    {
        const int POLL_DELAY = 250;
        WebBrowser _webBrowser;

        // set WebBrowser features, more info: http://stackoverflow.com/a/18333982/1768303
        static void SetWebBrowserFeatures()
        {
            // don't change the registry if running in-proc inside Visual Studio
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;

            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

            var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";

            Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
                appName, GetBrowserEmulationMode(), RegistryValueKind.DWord);

            // enable the features which are "On" for the full Internet Explorer browser

            Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
                appName, 1, RegistryValueKind.DWord);

            Registry.SetValue(featureControlRegKey + "FEATURE_AJAX_CONNECTIONEVENTS",
                appName, 1, RegistryValueKind.DWord);

            Registry.SetValue(featureControlRegKey + "FEATURE_GPU_RENDERING",
                appName, 1, RegistryValueKind.DWord);

            Registry.SetValue(featureControlRegKey + "FEATURE_WEBOC_DOCUMENT_ZOOM",
                appName, 1, RegistryValueKind.DWord);

            Registry.SetValue(featureControlRegKey + "FEATURE_NINPUT_LEGACYMODE",
                appName, 0, RegistryValueKind.DWord);
        }

        static UInt32 GetBrowserEmulationMode()
        {
            int browserVersion = 0;
            using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
                RegistryKeyPermissionCheck.ReadSubTree,
                System.Security.AccessControl.RegistryRights.QueryValues))
            {
                var version = ieKey.GetValue("svcVersion");
                if (null == version)
                {
                    version = ieKey.GetValue("Version");
                    if (null == version)
                        throw new ApplicationException("Microsoft Internet Explorer is required!");
                }
                int.TryParse(version.ToString().Split('.')[0], out browserVersion);
            }

            if (browserVersion < 7)
            {
                throw new ApplicationException("Unsupported version of Microsoft Internet Explorer!");
            }

            UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. 

            switch (browserVersion)
            {
                case 7:
                    mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. 
                    break;
                case 8:
                    mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. 
                    break;
                case 9:
                    mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.                    
                    break;
                case 10:
                    mode = 10000; // Internet Explorer 10.
                    break;
            }

            return mode;
        }

        // static constructor, runs first
        static MainForm()
        {
            SetWebBrowserFeatures();
        }

        public MainForm()
        {
            InitializeComponent();

            _webBrowser = new WebBrowser() { Dock = DockStyle.Fill };
            this.Controls.Add(_webBrowser);

            this.Size = new System.Drawing.Size(800, 600);
            this.Load += MainForm_Load;
        }

        // start the task
        async void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                dynamic document = await LoadDynamicPage("http://demos.dojotoolkit.org/demos/calendar/demo.html",
                    CancellationToken.None);

                MessageBox.Show(new { document.documentMode, document.compatMode }.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // navigate and download 
        async Task<object> LoadDynamicPage(string url, CancellationToken token)
        {
            // navigate and await DocumentCompleted
            var tcs = new TaskCompletionSource<bool>();
            WebBrowserDocumentCompletedEventHandler handler = (s, arg) =>
                tcs.TrySetResult(true);

            using (token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false))
            {
                this._webBrowser.DocumentCompleted += handler;
                try
                {
                    this._webBrowser.Navigate(url);
                    await tcs.Task; // wait for DocumentCompleted
                }
                finally
                {
                    this._webBrowser.DocumentCompleted -= handler;
                }
            }

            // get the root element
            var documentElement = this._webBrowser.Document.GetElementsByTagName("html")[0];

            // poll the current HTML for changes asynchronosly
            var html = documentElement.OuterHtml;
            while (true)
            {
                // wait asynchronously, this will throw if cancellation requested
                await Task.Delay(POLL_DELAY, token);

                // continue polling if the WebBrowser is still busy
                if (this._webBrowser.IsBusy)
                    continue;

                var htmlNow = documentElement.OuterHtml;
                if (html == htmlNow)
                    break; // no changes detected, end the poll loop

                html = htmlNow;
            }

            // consider the page fully rendered 
            token.ThrowIfCancellationRequested();

            return this._webBrowser.Document.DomDocument;
        }
    }
}

这篇关于Web浏览器控制仿真问题(FEATURE_BROWSER_EMULATION)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆