Skip to main content
Bumped by Community user
deleted 24 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

An Extended WebBrowser Controlcontrol

Without further ado:

An Extended WebBrowser Control

Without further ado:

Extended WebBrowser control

added 20 characters in body
Source Link
Der Kommissar
  • 20.1k
  • 4
  • 68
  • 158
/// <summary>
/// Extends the <code>System.Windows.Forms.WebBrowser</code> and adds the IsClosing event, as well as some JavaScript manipulation. It also adds some configuration that can be used to customize the operation of the control.
/// </summary>
public class ExtendedWebBrowser : WebBrowser
{
    private const int WM_PARENTNOTIFY = 528;
    private const int WM_DESTROY = 2;

    /// <summary>
    /// This event is called anytime the WebBrowser is set to destroy itself, including Javascript window.close() calls.
    /// </summary>
    public event EventHandler<EventArgs> IsClosing;

    /// <summary>
    /// Overrides the default <see cref="WebBrowser.WndProc(ref Message)"/> method.
    /// </summary>
    /// <param name="m">The <code>System.Windows.Forms.Message</code> being sent to the control.</param>
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PARENTNOTIFY)
        {
            if (!DesignMode && m.WParam.ToInt32() == WM_DESTROY)
            {
                IsClosing(this, new EventArgs());
            }

            DefWndProc(ref m);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    /// <summary>
    /// Injects some JavaScript to the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="javaScript">The code to inject.</param>
    /// <param name="name">The name of the script.</param>
    public HtmlElement InjectScript(string javaScript, string name)
    {
        var head = Document.GetElementsByTagName("head")[0];
        var scriptElement = Document.CreateElement("script");

        scriptElement.Name = name;
        scriptElement.SetAttribute("text", javaScript);

        head.AppendChild(scriptElement);

        return scriptElement;
    }

    /// <summary>
    /// Executes a JavaScript method in the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="scriptName">The name of the method to invoke.</param>
    /// <param name="args">The arguments to pass to the method.</param>
    public void ExecuteScript(string scriptName, params object[] args)
    {
        Document.InvokeScript(scriptName, args);
    }

    /// <summary>
    /// Executes arbitrary JavaScript code.
    /// </summary>
    /// <param name="script">The code to invoke.</param>
    /// <remarks>
    /// This creates an element with the name <code>arbitrary-script</code> with a javascript method called <code>arbitraryScript</code>. If a method with this name is present in the document, this method may not have the desired effect. If an element with the aforementioned name is present, it will be altered.
    /// </remarks>
    public void ExecuteArbitraryJavaScript(string script)
    {
        var arbitraryScript = Document.GetElementById("arbitrary-script");

        if (arbitraryScript == null)
        {
            arbitraryScript = InjectScript("function arbitraryScript() { " + script + " }", "arbitrary-script");
        }
        else
        {
            arbitraryScript.SetAttribute("text", "function arbitraryScript() { " + script + " }");
        }

        ExecuteScript("arbitraryScript");

        arbitraryScript.SetAttribute("text", "");
    }
    
    private void SetDWordKey(string baseKey, string name, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(baseKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(name, value, RegistryValueKind.DWord);
        }
    }

    /// <summary>
    /// Initializes the <see cref="ExtendedWebBrowser"/> with the provided configuration.
    /// </summary>
    /// <param name="config">The <see cref="ExtendedWebBrowserConfiguration"/> to update the registry with.</param>
    /// <remarks>
    /// This method will update the registry values of all properties with the <see cref="FeatureControlAttribute"/> on them, of the specified key to the provided value in the configuration object.
    /// 
    /// See: http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx for more information on Feature Controls.
    /// </remarks>
    public void Initialize(ExtendedWebBrowserConfiguration config)
    {
        var name = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
        
        if (name.ToLower() == "devenv.exe" || name.ToLower() == "xdesproc.exe")
        {
            return;
        }
        
        foreach (var propproperty in config.GetType().GetProperties().Where(propproperty => Attribute.IsDefined(propproperty, typeof(FeatureControlAttribute))))
        {
            var attribute = (FeatureControlAttribute)propproperty.GetCustomAttributes(typeof(FeatureControlAttribute), true)[0];
            var rawValue = propproperty.GetValue(config);
            var cleanValue = 0u;

            if (rawValue is bool)
            {
                cleanValue = (bool)rawValue ? 1u : 0u;
            }
            else
            {
                cleanValue = (uint)rawValue;
            }

            SetDWordKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\" + attribute.Key, name, cleanValue);
        }
    }
}
/// <summary>
/// Extends the <code>System.Windows.Forms.WebBrowser</code> and adds the IsClosing event, as well as some JavaScript manipulation. It also adds some configuration that can be used to customize the operation of the control.
/// </summary>
public class ExtendedWebBrowser : WebBrowser
{
    private const int WM_PARENTNOTIFY = 528;
    private const int WM_DESTROY = 2;

    /// <summary>
    /// This event is called anytime the WebBrowser is set to destroy itself, including Javascript window.close() calls.
    /// </summary>
    public event EventHandler<EventArgs> IsClosing;

    /// <summary>
    /// Overrides the default <see cref="WebBrowser.WndProc(ref Message)"/> method.
    /// </summary>
    /// <param name="m">The <code>System.Windows.Forms.Message</code> being sent to the control.</param>
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PARENTNOTIFY)
        {
            if (!DesignMode && m.WParam.ToInt32() == WM_DESTROY)
            {
                IsClosing(this, new EventArgs());
            }

            DefWndProc(ref m);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    /// <summary>
    /// Injects some JavaScript to the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="javaScript">The code to inject.</param>
    /// <param name="name">The name of the script.</param>
    public HtmlElement InjectScript(string javaScript, string name)
    {
        var head = Document.GetElementsByTagName("head")[0];
        var scriptElement = Document.CreateElement("script");

        scriptElement.Name = name;
        scriptElement.SetAttribute("text", javaScript);

        head.AppendChild(scriptElement);

        return scriptElement;
    }

    /// <summary>
    /// Executes a JavaScript method in the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="scriptName">The name of the method to invoke.</param>
    /// <param name="args">The arguments to pass to the method.</param>
    public void ExecuteScript(string scriptName, params object[] args)
    {
        Document.InvokeScript(scriptName, args);
    }

    /// <summary>
    /// Executes arbitrary JavaScript code.
    /// </summary>
    /// <param name="script">The code to invoke.</param>
    /// <remarks>
    /// This creates an element with the name <code>arbitrary-script</code> with a javascript method called <code>arbitraryScript</code>. If a method with this name is present in the document, this method may not have the desired effect. If an element with the aforementioned name is present, it will be altered.
    /// </remarks>
    public void ExecuteArbitraryJavaScript(string script)
    {
        var arbitraryScript = Document.GetElementById("arbitrary-script");

        if (arbitraryScript == null)
        {
            arbitraryScript = InjectScript("function arbitraryScript() { " + script + " }", "arbitrary-script");
        }
        else
        {
            arbitraryScript.SetAttribute("text", "function arbitraryScript() { " + script + " }");
        }

        ExecuteScript("arbitraryScript");

        arbitraryScript.SetAttribute("text", "");
    }
    
    private void SetDWordKey(string baseKey, string name, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(baseKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(name, value, RegistryValueKind.DWord);
        }
    }

    /// <summary>
    /// Initializes the <see cref="ExtendedWebBrowser"/> with the provided configuration.
    /// </summary>
    /// <param name="config">The <see cref="ExtendedWebBrowserConfiguration"/> to update the registry with.</param>
    /// <remarks>
    /// This method will update the registry values of all properties with the <see cref="FeatureControlAttribute"/> on them, of the specified key to the provided value in the configuration object.
    /// 
    /// See: http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx for more information on Feature Controls.
    /// </remarks>
    public void Initialize(ExtendedWebBrowserConfiguration config)
    {
        var name = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
        
        if (name.ToLower() == "devenv.exe" || name.ToLower() == "xdesproc.exe")
        {
            return;
        }
        
        foreach (var prop in config.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FeatureControlAttribute))))
        {
            var attribute = (FeatureControlAttribute)prop.GetCustomAttributes(typeof(FeatureControlAttribute), true)[0];
            var rawValue = prop.GetValue(config);
            var cleanValue = 0u;

            if (rawValue is bool)
            {
                cleanValue = (bool)rawValue ? 1u : 0u;
            }
            else
            {
                cleanValue = (uint)rawValue;
            }

            SetDWordKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\" + attribute.Key, name, cleanValue);
        }
    }
}
/// <summary>
/// Extends the <code>System.Windows.Forms.WebBrowser</code> and adds the IsClosing event, as well as some JavaScript manipulation. It also adds some configuration that can be used to customize the operation of the control.
/// </summary>
public class ExtendedWebBrowser : WebBrowser
{
    private const int WM_PARENTNOTIFY = 528;
    private const int WM_DESTROY = 2;

    /// <summary>
    /// This event is called anytime the WebBrowser is set to destroy itself, including Javascript window.close() calls.
    /// </summary>
    public event EventHandler<EventArgs> IsClosing;

    /// <summary>
    /// Overrides the default <see cref="WebBrowser.WndProc(ref Message)"/> method.
    /// </summary>
    /// <param name="m">The <code>System.Windows.Forms.Message</code> being sent to the control.</param>
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PARENTNOTIFY)
        {
            if (!DesignMode && m.WParam.ToInt32() == WM_DESTROY)
            {
                IsClosing(this, new EventArgs());
            }

            DefWndProc(ref m);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    /// <summary>
    /// Injects some JavaScript to the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="javaScript">The code to inject.</param>
    /// <param name="name">The name of the script.</param>
    public HtmlElement InjectScript(string javaScript, string name)
    {
        var head = Document.GetElementsByTagName("head")[0];
        var scriptElement = Document.CreateElement("script");

        scriptElement.Name = name;
        scriptElement.SetAttribute("text", javaScript);

        head.AppendChild(scriptElement);

        return scriptElement;
    }

    /// <summary>
    /// Executes a JavaScript method in the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="scriptName">The name of the method to invoke.</param>
    /// <param name="args">The arguments to pass to the method.</param>
    public void ExecuteScript(string scriptName, params object[] args)
    {
        Document.InvokeScript(scriptName, args);
    }

    /// <summary>
    /// Executes arbitrary JavaScript code.
    /// </summary>
    /// <param name="script">The code to invoke.</param>
    /// <remarks>
    /// This creates an element with the name <code>arbitrary-script</code> with a javascript method called <code>arbitraryScript</code>. If a method with this name is present in the document, this method may not have the desired effect. If an element with the aforementioned name is present, it will be altered.
    /// </remarks>
    public void ExecuteArbitraryJavaScript(string script)
    {
        var arbitraryScript = Document.GetElementById("arbitrary-script");

        if (arbitraryScript == null)
        {
            arbitraryScript = InjectScript("function arbitraryScript() { " + script + " }", "arbitrary-script");
        }
        else
        {
            arbitraryScript.SetAttribute("text", "function arbitraryScript() { " + script + " }");
        }

        ExecuteScript("arbitraryScript");

        arbitraryScript.SetAttribute("text", "");
    }
    
    private void SetDWordKey(string baseKey, string name, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(baseKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(name, value, RegistryValueKind.DWord);
        }
    }

    /// <summary>
    /// Initializes the <see cref="ExtendedWebBrowser"/> with the provided configuration.
    /// </summary>
    /// <param name="config">The <see cref="ExtendedWebBrowserConfiguration"/> to update the registry with.</param>
    /// <remarks>
    /// This method will update the registry values of all properties with the <see cref="FeatureControlAttribute"/> on them, of the specified key to the provided value in the configuration object.
    /// 
    /// See: http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx for more information on Feature Controls.
    /// </remarks>
    public void Initialize(ExtendedWebBrowserConfiguration config)
    {
        var name = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
        
        if (name.ToLower() == "devenv.exe" || name.ToLower() == "xdesproc.exe")
        {
            return;
        }
        
        foreach (var property in config.GetType().GetProperties().Where(property => Attribute.IsDefined(property, typeof(FeatureControlAttribute))))
        {
            var attribute = (FeatureControlAttribute)property.GetCustomAttributes(typeof(FeatureControlAttribute), true)[0];
            var rawValue = property.GetValue(config);
            var cleanValue = 0u;

            if (rawValue is bool)
            {
                cleanValue = (bool)rawValue ? 1u : 0u;
            }
            else
            {
                cleanValue = (uint)rawValue;
            }

            SetDWordKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\" + attribute.Key, name, cleanValue);
        }
    }
}
deleted 4 characters in body
Source Link
Der Kommissar
  • 20.1k
  • 4
  • 68
  • 158
/// <summary>
/// Extends the <code>System.Windows.Forms.WebBrowser</code> and adds the IsClosing event, as well as some JavaScript manipulation. It also adds some configuration that can be used to customize the operation of the control.
/// </summary>
public class ExtendedWebBrowser : WebBrowser
{
    private const int WM_PARENTNOTIFY = 528;
    private const int WM_DESTROY = 2;

    /// <summary>
    /// This event is called anytime the WebBrowser is set to destroy itself, including Javascript window.close() calls.
    /// </summary>
    public event EventHandler<EventArgs> IsClosing;

    /// <summary>
    /// Overrides the default <see cref="ExtendedWebBrowsercref="WebBrowser.WndProc(ref Message)"/> method.
    /// </summary>
    /// <param name="m">The <code>System.Windows.Forms.Message</code> being sent to the control.</param>
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PARENTNOTIFY)
        {
            if (!DesignMode && m.WParam.ToInt32() == WM_DESTROY)
            {
                IsClosing(this, new EventArgs());
            }

            DefWndProc(ref m);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    /// <summary>
    /// Injects some JavaScript to the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="javaScript">The code to inject.</param>
    /// <param name="name">The name of the script.</param>
    public HtmlElement InjectScript(string javaScript, string name)
    {
        var head = Document.GetElementsByTagName("head")[0];
        var scriptElement = Document.CreateElement("script");

        scriptElement.Name = name;
        scriptElement.SetAttribute("text", javaScript);

        head.AppendChild(scriptElement);

        return scriptElement;
    }

    /// <summary>
    /// Executes a JavaScript method in the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="scriptName">The name of the method to invoke.</param>
    /// <param name="args">The arguments to pass to the method.</param>
    public void ExecuteScript(string scriptName, params object[] args)
    {
        Document.InvokeScript(scriptName, args);
    }

    /// <summary>
    /// Executes arbitrary JavaScript code.
    /// </summary>
    /// <param name="script">The code to invoke.</param>
    /// <remarks>
    /// This creates an element with the name <code>arbitrary-script</code> with a javascript method called <code>arbitraryScript</code>. If a method with this name is present in the document, this method may not have the desired effect. If an element with the aforementioned name is present, it will be altered.
    /// </remarks>
    public void ExecuteArbitraryJavaScript(string script)
    {
        var arbitraryScript = Document.GetElementById("arbitrary-script");

        if (arbitraryScript == null)
        {
            arbitraryScript = InjectScript("function arbitraryScript() { " + script + " }", "arbitrary-script");
        }
        else
        {
            arbitraryScript.SetAttribute("text", "function arbitraryScript() { " + script + " }");
        }

        ExecuteScript("arbitraryScript");

        arbitraryScript.SetAttribute("text", "");
    }
    
    private void SetDWordKey(string baseKey, string name, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(baseKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(name, value, RegistryValueKind.DWord);
        }
    }

    /// <summary>
    /// Initializes the <see cref="ExtendedWebBrowser"/> with the provided configuration.
    /// </summary>
    /// <param name="config">The <see cref="ExtendedWebBrowserConfiguration"/> to update the registry with.</param>
    /// <remarks>
    /// This method will update the registry values of all properties with the <see cref="FeatureControlAttribute"/> on them, of the specified key to the provided value in the configuration object.
    /// 
    /// See: http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx for more information on Feature Controls.
    /// </remarks>
    public void Initialize(ExtendedWebBrowserConfiguration config)
    {
        var name = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
        
        if (name.ToLower() == "devenv.exe" || name.ToLower() == "xdesproc.exe")
        {
            return;
        }
        
        foreach (var prop in config.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FeatureControlAttribute))))
        {
            var attribute = (FeatureControlAttribute)prop.GetCustomAttributes(typeof(FeatureControlAttribute), true)[0];
            var rawValue = prop.GetValue(config);
            var cleanValue = 0u;

            if (rawValue is bool)
            {
                cleanValue = (bool)rawValue ? 1u : 0u;
            }
            else
            {
                cleanValue = (uint)rawValue;
            }

            SetDWordKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\" + attribute.Key, name, cleanValue);
        }
    }
}
/// <summary>
/// Extends the <code>System.Windows.Forms.WebBrowser</code> and adds the IsClosing event, as well as some JavaScript manipulation. It also adds some configuration that can be used to customize the operation of the control.
/// </summary>
public class ExtendedWebBrowser : WebBrowser
{
    private const int WM_PARENTNOTIFY = 528;
    private const int WM_DESTROY = 2;

    /// <summary>
    /// This event is called anytime the WebBrowser is set to destroy itself, including Javascript window.close() calls.
    /// </summary>
    public event EventHandler<EventArgs> IsClosing;

    /// <summary>
    /// Overrides the default <see cref="ExtendedWebBrowser.WndProc(Message)"/> method.
    /// </summary>
    /// <param name="m">The <code>System.Windows.Forms.Message</code> being sent to the control.</param>
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PARENTNOTIFY)
        {
            if (!DesignMode && m.WParam.ToInt32() == WM_DESTROY)
            {
                IsClosing(this, new EventArgs());
            }

            DefWndProc(ref m);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    /// <summary>
    /// Injects some JavaScript to the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="javaScript">The code to inject.</param>
    /// <param name="name">The name of the script.</param>
    public HtmlElement InjectScript(string javaScript, string name)
    {
        var head = Document.GetElementsByTagName("head")[0];
        var scriptElement = Document.CreateElement("script");

        scriptElement.Name = name;
        scriptElement.SetAttribute("text", javaScript);

        head.AppendChild(scriptElement);

        return scriptElement;
    }

    /// <summary>
    /// Executes a JavaScript method in the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="scriptName">The name of the method to invoke.</param>
    /// <param name="args">The arguments to pass to the method.</param>
    public void ExecuteScript(string scriptName, params object[] args)
    {
        Document.InvokeScript(scriptName, args);
    }

    /// <summary>
    /// Executes arbitrary JavaScript code.
    /// </summary>
    /// <param name="script">The code to invoke.</param>
    /// <remarks>
    /// This creates an element with the name <code>arbitrary-script</code> with a javascript method called <code>arbitraryScript</code>. If a method with this name is present in the document, this method may not have the desired effect. If an element with the aforementioned name is present, it will be altered.
    /// </remarks>
    public void ExecuteArbitraryJavaScript(string script)
    {
        var arbitraryScript = Document.GetElementById("arbitrary-script");

        if (arbitraryScript == null)
        {
            arbitraryScript = InjectScript("function arbitraryScript() { " + script + " }", "arbitrary-script");
        }
        else
        {
            arbitraryScript.SetAttribute("text", "function arbitraryScript() { " + script + " }");
        }

        ExecuteScript("arbitraryScript");

        arbitraryScript.SetAttribute("text", "");
    }
    
    private void SetDWordKey(string baseKey, string name, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(baseKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(name, value, RegistryValueKind.DWord);
        }
    }

    /// <summary>
    /// Initializes the <see cref="ExtendedWebBrowser"/> with the provided configuration.
    /// </summary>
    /// <param name="config">The <see cref="ExtendedWebBrowserConfiguration"/> to update the registry with.</param>
    /// <remarks>
    /// This method will update the registry values of all properties with the <see cref="FeatureControlAttribute"/> on them, of the specified key to the provided value in the configuration object.
    /// 
    /// See: http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx for more information on Feature Controls.
    /// </remarks>
    public void Initialize(ExtendedWebBrowserConfiguration config)
    {
        var name = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
        
        if (name.ToLower() == "devenv.exe" || name.ToLower() == "xdesproc.exe")
        {
            return;
        }
        
        foreach (var prop in config.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FeatureControlAttribute))))
        {
            var attribute = (FeatureControlAttribute)prop.GetCustomAttributes(typeof(FeatureControlAttribute), true)[0];
            var rawValue = prop.GetValue(config);
            var cleanValue = 0u;

            if (rawValue is bool)
            {
                cleanValue = (bool)rawValue ? 1u : 0u;
            }
            else
            {
                cleanValue = (uint)rawValue;
            }

            SetDWordKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\" + attribute.Key, name, cleanValue);
        }
    }
}
/// <summary>
/// Extends the <code>System.Windows.Forms.WebBrowser</code> and adds the IsClosing event, as well as some JavaScript manipulation. It also adds some configuration that can be used to customize the operation of the control.
/// </summary>
public class ExtendedWebBrowser : WebBrowser
{
    private const int WM_PARENTNOTIFY = 528;
    private const int WM_DESTROY = 2;

    /// <summary>
    /// This event is called anytime the WebBrowser is set to destroy itself, including Javascript window.close() calls.
    /// </summary>
    public event EventHandler<EventArgs> IsClosing;

    /// <summary>
    /// Overrides the default <see cref="WebBrowser.WndProc(ref Message)"/> method.
    /// </summary>
    /// <param name="m">The <code>System.Windows.Forms.Message</code> being sent to the control.</param>
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PARENTNOTIFY)
        {
            if (!DesignMode && m.WParam.ToInt32() == WM_DESTROY)
            {
                IsClosing(this, new EventArgs());
            }

            DefWndProc(ref m);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    /// <summary>
    /// Injects some JavaScript to the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="javaScript">The code to inject.</param>
    /// <param name="name">The name of the script.</param>
    public HtmlElement InjectScript(string javaScript, string name)
    {
        var head = Document.GetElementsByTagName("head")[0];
        var scriptElement = Document.CreateElement("script");

        scriptElement.Name = name;
        scriptElement.SetAttribute("text", javaScript);

        head.AppendChild(scriptElement);

        return scriptElement;
    }

    /// <summary>
    /// Executes a JavaScript method in the <see cref="ExtendedWebBrowser"/>.
    /// </summary>
    /// <param name="scriptName">The name of the method to invoke.</param>
    /// <param name="args">The arguments to pass to the method.</param>
    public void ExecuteScript(string scriptName, params object[] args)
    {
        Document.InvokeScript(scriptName, args);
    }

    /// <summary>
    /// Executes arbitrary JavaScript code.
    /// </summary>
    /// <param name="script">The code to invoke.</param>
    /// <remarks>
    /// This creates an element with the name <code>arbitrary-script</code> with a javascript method called <code>arbitraryScript</code>. If a method with this name is present in the document, this method may not have the desired effect. If an element with the aforementioned name is present, it will be altered.
    /// </remarks>
    public void ExecuteArbitraryJavaScript(string script)
    {
        var arbitraryScript = Document.GetElementById("arbitrary-script");

        if (arbitraryScript == null)
        {
            arbitraryScript = InjectScript("function arbitraryScript() { " + script + " }", "arbitrary-script");
        }
        else
        {
            arbitraryScript.SetAttribute("text", "function arbitraryScript() { " + script + " }");
        }

        ExecuteScript("arbitraryScript");

        arbitraryScript.SetAttribute("text", "");
    }
    
    private void SetDWordKey(string baseKey, string name, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(baseKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(name, value, RegistryValueKind.DWord);
        }
    }

    /// <summary>
    /// Initializes the <see cref="ExtendedWebBrowser"/> with the provided configuration.
    /// </summary>
    /// <param name="config">The <see cref="ExtendedWebBrowserConfiguration"/> to update the registry with.</param>
    /// <remarks>
    /// This method will update the registry values of all properties with the <see cref="FeatureControlAttribute"/> on them, of the specified key to the provided value in the configuration object.
    /// 
    /// See: http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx for more information on Feature Controls.
    /// </remarks>
    public void Initialize(ExtendedWebBrowserConfiguration config)
    {
        var name = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
        
        if (name.ToLower() == "devenv.exe" || name.ToLower() == "xdesproc.exe")
        {
            return;
        }
        
        foreach (var prop in config.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FeatureControlAttribute))))
        {
            var attribute = (FeatureControlAttribute)prop.GetCustomAttributes(typeof(FeatureControlAttribute), true)[0];
            var rawValue = prop.GetValue(config);
            var cleanValue = 0u;

            if (rawValue is bool)
            {
                cleanValue = (bool)rawValue ? 1u : 0u;
            }
            else
            {
                cleanValue = (uint)rawValue;
            }

            SetDWordKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\" + attribute.Key, name, cleanValue);
        }
    }
}
edited body
Source Link
Der Kommissar
  • 20.1k
  • 4
  • 68
  • 158
Loading
Source Link
Der Kommissar
  • 20.1k
  • 4
  • 68
  • 158
Loading