Download IeBrowserEmulation.zip, last updated 28/06/2014 (18.63 KB)

Download
  • md5: 565fb8296871228bd35c3b3daa58d187
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Cyotek.ApplicationServices.Windows.Forms;

namespace IeBrowserEmulation
{
  // Configuring the emulation mode of an Internet Explorer WebBrowser control
  // http://cyotek.com/blog/configuring-the-emulation-mode-of-an-internet-explorer-webbrowser-control

  public partial class MainForm : Form
  {
    #region Instance Fields

    private WebBrowser _webBrowser;

    #endregion

    #region Public Constructors

    public MainForm()
    {
      InitializeComponent();
    }

    #endregion

    #region Overridden Methods

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        this.RemoveWebBrowser();

        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose(disposing);
    }

    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
    protected override void OnLoad(EventArgs e)
    {
      int internetExplorerMajorVersion;

      base.OnLoad(e);

      this.Font = SystemFonts.MessageBoxFont;
      ieVersionLabel.Font = new Font(this.Font.FontFamily, 20F, FontStyle.Bold);

      // show the ie version which will help explain why any controls are disabled
      internetExplorerMajorVersion = InternetExplorerBrowserEmulation.GetInternetExplorerMajorVersion();
      ieVersionLabel.Text = internetExplorerMajorVersion.ToString(CultureInfo.InvariantCulture);

      // use reflection to build the radio controls, so I don't need to do anything when adding new values to the enum
      this.BuildEmulationRadioButtonList();

      // update the preview
      this.UpdatePreview(InternetExplorerBrowserEmulation.GetBrowserEmulationVersion());
    }

    #endregion

    #region Private Members

    private void BuildEmulationRadioButtonList()
    {
      int x;
      int y;
      BrowserEmulationVersion currentEmulationVersion;

      currentEmulationVersion = InternetExplorerBrowserEmulation.GetBrowserEmulationVersion();

      x = emulationGroupBox.DisplayRectangle.X;
      y = emulationGroupBox.DisplayRectangle.Y;

      foreach (BrowserEmulationVersion version in Enum.GetValues(typeof(BrowserEmulationVersion)))
      {
        RadioButton control;

        control = new RadioButton
                  {
                    Text = version.ToString(),
                    Tag = version,
                    AutoSize = true,
                    Left = x,
                    Top = y,
                    Checked = (version == currentEmulationVersion)
                  };

        control.Click += this.EmulationVersionRadioButtonClickHandler;

        emulationGroupBox.Controls.Add(control);
        y += (control.Height + control.Margin.Bottom);
      }

      // move the restart warning below the list
      restartWarningLabel.Top = y + restartWarningLabel.Margin.Top;
    }

    private void RemoveWebBrowser()
    {
      if (_webBrowser != null)
      {
        if (_webBrowser.Parent != null)
        {
          _webBrowser.Parent.Controls.Remove(_webBrowser);
        }

        _webBrowser.Dispose();
        _webBrowser = null;
      }
    }

    private void UpdatePreview(BrowserEmulationVersion version)
    {
      this.RemoveWebBrowser();

      _webBrowser = new WebBrowser
                    {
                      Dock = DockStyle.Fill
                    };

      previewGroupBox.Controls.Add(_webBrowser);

      _webBrowser.Navigate(new Uri("http://cyotek.com/"));
    }

    #endregion

    #region Event Handlers

    private void EmulationVersionRadioButtonClickHandler(object sender, EventArgs e)
    {
      RadioButton button;
      BrowserEmulationVersion version;

      button = (RadioButton)sender;
      version = (BrowserEmulationVersion)button.Tag;

      if (InternetExplorerBrowserEmulation.GetBrowserEmulationVersion() != version)
      {
        // apply the new emulation version
        if (!InternetExplorerBrowserEmulation.SetBrowserEmulationVersion(version))
        {
          MessageBox.Show("Failed to update browser emulation version.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else
        {
          // now destroy and recreate the WebBrowser control
          Application.Restart();
          Environment.Exit(-1);
        }
      }
    }

    private void closeButton_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    #endregion
  }
}

Donate

Donate