Use latest version of Internet Explorer in the webbrowser control

后端 未结 13 1423
长发绾君心
长发绾君心 2020-11-21 23:29

The default version of the webbrowser control in a C# Windows Forms application is 7. I have changed to 9 by the article Browser Emulation, but how is it possible t

13条回答
  •  一向
    一向 (楼主)
    2020-11-22 00:16

    I was able to implement Luca's solution, but I had to make a few changes for it to work. My goal was to use D3.js with a Web Browser control for a Windows Forms Application (targeting .NET 2.0). It is working for me now. I hope this can help someone else.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;
    using Microsoft.Win32;
    using System.Diagnostics;
    
    namespace ClientUI
    {
        static class Program
        {
            static Mutex mutex = new System.Threading.Mutex(false, "jMutex");
    
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main()
            {
                if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
                {
                    // Another application instance is running
                    return;
                }
                try
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
    
                    var targetApplication = Process.GetCurrentProcess().ProcessName + ".exe";
                    int ie_emulation = 11999;
                    try
                    {
                        string tmp = Properties.Settings.Default.ie_emulation;
                        ie_emulation = int.Parse(tmp);
                    }
                    catch { }
                    SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation);
    
                    Application.Run(new MainForm());
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
    
            private static void SetIEVersioneKeyforWebBrowserControl(string appName, int ieval)
            {
                RegistryKey Regkey = null;
                try
                {
                    Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
    
                    // If the path is not correct or
                    // if user doesn't have privileges to access the registry
                    if (Regkey == null)
                    {
                        MessageBox.Show("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found");
                        return;
                    }
    
                    string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
    
                    // Check if key is already present
                    if (FindAppkey == ieval.ToString())
                    {
                        MessageBox.Show("Application FEATURE_BROWSER_EMULATION already set to " + ieval);
                        Regkey.Close();
                        return;
                    }
    
                    // If key is not present or different from desired, add/modify the key , key value
                    Regkey.SetValue(appName, unchecked((int)ieval), RegistryValueKind.DWord);
    
                    // Check for the key after adding
                    FindAppkey = Convert.ToString(Regkey.GetValue(appName));
    
                    if (FindAppkey == ieval.ToString())
                    {
                        MessageBox.Show("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart");
                    }
                    else
                    {
                        MessageBox.Show("Application FEATURE_BROWSER_EMULATION setting failed; current value is  " + ieval);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message);
                }
                finally
                {
                    //Close the Registry
                    if (Regkey != null) Regkey.Close();
                }
            }
        }
    }
    

    Also, I added a string (ie_emulation) to the project's settings with the value of 11999. This value seems to be working for IE11(11.0.15).

    Next, I had to change the permission for my application to allow access to the registry. This can be done by adding a new item to your project (using VS2012). Under the General Items, select Application Manifest File. Change the level from asInvoker to requireAdministrator (as shown below).

    
    

    If someone reading this is trying to use D3.js with a webbrowser control, you may have to modify the JSON data to be stored within a variable inside your HTML page because D3.json uses XmlHttpRequest (easier to use with a webserver). After those changes and the above, my windows forms are able to load local HTML files that call D3.

提交回复
热议问题