代码之家  ›  专栏  ›  技术社区  ›  Congree

Web浏览器,用于处理应用程序中的弹出窗口

  •  4
  • Congree  · 技术社区  · 11 年前

    我正在尝试使用WebBrowser控件为弹出窗口启动一个新的窗体,而不是在IE中打开。我尝试使用AxWebBrowser来获取与NewWindow3事件一起工作的弹出窗口 e.ppDisp = AxWebBrowser.Application ,但AxWebBrowser有许多限制。因此,我尝试扩展普通的WebBrowser以包括NewWindow3事件,就像AxWebBrowser一样,但遇到了问题。具有 e.ppDisp=AxWebBrowser应用程序 我收到错误:如果我继续,“检测到InvalidVariant”,然后是“指定的OLE变体无效”。

    注意:这是我第一次延长课程,所以我可能会过于简单。如果我尝试在新窗口中导航到新的URL,我会从网站上得到java脚本错误。

    我已经根据评论更新了代码。已经删除了ExtendedWebBrowser类,以获得更小更好的版本。这是新代码:

    从主窗体和非常相似的BrowserPopup窗体-

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        nativeBrowser = DirectCast(ExtendedWebBrowser1.ActiveXInstance, SHDocVw.WebBrowser)
        AddHandler nativeBrowser.NewWindow3, AddressOf nativeBrowser_NewWindow3
        AddHandler nativeBrowser.WindowClosing, AddressOf nativeBrowser_WindowClosing
    End Sub
    
    Private Sub nativeBrowser_NewWindow3(ByRef ppDisp As Object, ByRef Cancel As Boolean, ByVal dwflags As UInteger, ByVal bStrUrlContext As String, ByVal bstrUrl As String)
        Dim popup = New BrowserPopup()
        popup.Show(Me)
        popup.browserPop.DocumentText = bStrUrlContext
        ppDisp = popup.browserPop.ActiveXInstance
    End Sub
    
    Private Sub nativeBrowser_WindowClosing(ByVal IsChildWindow As Boolean, ByRef Cancel As Boolean)
        MsgBox("working?") '<<<Doesn't Trigger>>>
    End Sub
    
    Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
        MyBase.OnFormClosing(e)
    End Sub
    

    此外,如果有帮助的话,这里是页面中的脚本,它应该能够关闭弹出窗体,但似乎只是停用了WebBrowser。

    <table isListBtn="false" cellpadding="0" enabled="true" class="buttonBorderBlue"
    cellspacing="0" border="0" onClick="if (typeof(workpaneMediator_toolbar)!='undefined')
    workpaneMediator_toolbar.onSelect('CANCEL_ACTION', this)"
    actionType="CLOSE_WINDOW_TYPE" id="workpaneMediator_toolbar_CANCEL_ACTIONWrapper"
    nowrap><tr><td class="buttonBlueTD">
    
    1 回复  |  直到 11 年前
        1
  •  5
  •   noseratio    11 年前

    你说得对 WindowClosing 不会因为Winforms而被解雇 WebBrowser 控制,我确认。这似乎是.NET中的一个长期错误 控件 ActiveX托管代码,该代码从未被处理过。检查 this post 了解更多详细信息和可能的解决方法。

    另一种可能的解决方法是直接通过托管WebBrowser ActiveX控件 AxHost 类,在这种情况下 窗口关闭 正确开火。例子:

    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