Web Browser to handle pop ups within the application

后端 未结 1 1009
予麋鹿
予麋鹿 2021-01-14 00:08

I am trying to use the WebBrowser control to launch a new form for popups instead of it opening in IE. I have tried to use the AxWebBrowser instead to get the popups which w

相关标签:
1条回答
  • 2021-01-14 00:50

    You're right that WindowClosing doesn't get fired for Winforms WebBrowser Control, I confirm that. It appears to be a long-time bug in .NET WebBrowser ActiveX hosting code, which has never been addressed. Check this post for more details and a possible workaround.

    Another possible workaround may be to host WebBrowser ActiveX Control directly via AxHost class, in which case WindowClosing gets fire correctly. Example:

    C#:

    using Microsoft.Win32;
    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WebBrowserApp
    {
        // MainForm
    
        public partial class MainForm : Form 
        {
            WebBrowser webBrowser;
    
            public MainForm()
            {
                InitializeComponent();
                InitBrowser();
    
                if (this.webBrowser.Document == null && this.webBrowser.ActiveXInstance == null)
                    throw new ApplicationException ("Unable to initialize WebBrowser ActiveX control.");
    
                var ax = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
                ax.NewWindow2 += (ref object ppDisp, ref bool Cancel) =>
                {
                    var popup = new RawBrowserPopup();
                    popup.Visible = true;
                    ppDisp = popup.WebBrowser.ActiveXInstance;
                };
    
                this.Load += (s, e) =>
                {
                    this.webBrowser.DocumentText = "<a target=\"_blank\" href=\"javascript:'<button onclick=\\'window.close()\\'>Close</button>'\">Go</a>";
                };
            }
    
            // create a WebBrowser instance
            void InitBrowser()
            {
                this.webBrowser = new WebBrowser();
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
                this.webBrowser.Visible = true;
            }
        }
    
        // RawWebBrowser
    
        public class RawWebBrowser : AxHost
        {
            public RawWebBrowser()
                : base("8856f961-340a-11d0-a96b-00c04fd705a2")
            {
            }
    
            public event EventHandler Initialized;
    
            protected override void AttachInterfaces()
            {
                if (this.Initialized != null)
                    this.Initialized(this, new EventArgs());
            }
    
            public object ActiveXInstance
            {
                get
                {
                    return base.GetOcx();
                }
            }
        }
    
        // RawBrowserPopup
    
        public class RawBrowserPopup : Form
        {
            RawWebBrowser webBrowser;
    
            public RawWebBrowser WebBrowser
            {
                get { return this.webBrowser; }
            }
    
            public RawBrowserPopup()
            {
                this.webBrowser = new RawWebBrowser();
    
                this.webBrowser.Initialized += (s, e) =>
                {
                    var ax = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
                    ax.NewWindow2 += (ref object ppDisp, ref bool Cancel) =>
                    {
                        var popup = new RawBrowserPopup();
                        popup.Visible = true;
                        ppDisp = popup.WebBrowser.ActiveXInstance;
                    };
    
                    ax.WindowClosing += (bool IsChildWindow, ref bool Cancel) =>
                    {
                        Cancel = true;
                        this.Close();
                    };
                };
    
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
                this.webBrowser.Visible = true;
            }
        }
    }
    

    VB.NET:

    Public Class Form1
        Dim webBrowser As WebBrowser = New WebBrowser()
    
        Sub New()
            MyBase.New()
            Me.InitializeComponent()
            webBrowser.Dock = DockStyle.Fill
            Me.Controls.Add(webBrowser)
            webBrowser.Visible = True
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.webBrowser.DocumentText = "<a target='_blank' href='javascript:""<button onclick=\""window.close()\"">Close</button>""'>Go</a>"
    
            Dim ActiveX As SHDocVw.WebBrowser = Me.webBrowser.ActiveXInstance
            AddHandler ActiveX.NewWindow2, AddressOf WebBrowser_ActiveX_NewWindow2
        End Sub
    
        Private Sub WebBrowser_ActiveX_NewWindow2(ByRef ppDisp As Object, ByRef Cancel As Boolean)
            Dim popup As RawBrowserPopup = New RawBrowserPopup()
            popup.Visible = True
            ppDisp = popup.WebBrowser.ActiveXInstance
        End Sub
    End Class
    
    Public Class RawWebBrowser
        Inherits System.Windows.Forms.AxHost
    
        Sub New()
            MyBase.New("8856f961-340a-11d0-a96b-00c04fd705a2")
        End Sub
    
        Event Initialized(sender As Object, e As EventArgs)
    
        Protected Overrides Sub AttachInterfaces()
            RaiseEvent Initialized(Me, New EventArgs())
        End Sub
    
        Public ReadOnly Property ActiveXInstance() As Object
            Get
                Return MyBase.GetOcx()
            End Get
        End Property
    End Class
    
    Public Class RawBrowserPopup
        Inherits System.Windows.Forms.Form
    
        Dim WithEvents rawBrowser As RawWebBrowser = New RawWebBrowser()
    
        Sub New()
            MyBase.New()
            rawBrowser.Dock = DockStyle.Fill
            Me.Controls.Add(rawBrowser)
            rawBrowser.Visible = True
        End Sub
    
        Public ReadOnly Property WebBrowser() As Object
            Get
                Return rawBrowser
            End Get
        End Property
    
        Private Sub rawBrowser_Initialized(sender As Object, e As EventArgs) Handles rawBrowser.Initialized
            Dim activeX As SHDocVw.WebBrowser = rawBrowser.ActiveXInstance
            AddHandler activeX.NewWindow2, AddressOf activeX_NewWindow2
            AddHandler activeX.WindowClosing, AddressOf activeX_WindowClosing
        End Sub
    
        Private Sub activeX_NewWindow2(ByRef ppDisp As Object, ByRef Cancel As Boolean)
            Dim popup As RawBrowserPopup = New RawBrowserPopup()
            popup.Visible = True
            ppDisp = popup.WebBrowser.ActiveXInstance
        End Sub
    
        Private Sub activeX_WindowClosing(IsChildWindow As Boolean, ByRef Cancel As Boolean)
            Cancel = True
            Me.Close()
        End Sub
    
    End Class
    
    0 讨论(0)
提交回复
热议问题