GeckoFX浏览器 - 控制新窗口

时间:2017-05-05 15:38:58

标签: .net browser geckofx

我第一次使用 GeckoFX 工作。 我到处搜索,但没有回答有关它的问题。

我正在尝试在单击带有空白目标的href后弹出的新窗口中添加DocumentCompleted侦听器

<a href='newWindow.html' target='_blank'>Click here</a>

GeckoFX会创建一个新窗口并将其弹出,但我无法控制新窗口WebBrowser事件。

我尝试过像

这样的事情
    private void browser_CreateWindow(object sender, Gecko.GeckoCreateWindowEventArgs e)
    {
        e.WebBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
    }

    private void WebBrowser_DocumentCompleted(object sender, GeckoDocumentCompletedEventArgs e)
    {
        MessageBox.Show("Document Completed!");
    }

e.WebBrowser始终为空。

1 个答案:

答案 0 :(得分:0)

原始答案为here,但我会在链接死亡等情况下将其粘贴。

让我们说你就是这样:

 public partial class MainForm: Form
{
    public MainForm()
    {
        InitializeComponent();

        WindowWatcher.WindowCreator = new TabbedBrowserCreator( this );
        WindowWatcher.LockWindowCreator();

        gwBrowser.DocumentTitleChanged += gwBrowser_DocumentTitleChanged;
        gwBrowser.DocumentCompleted += gwBrowser_DocumentCompleted;

        gwBrowser.Navigate( "http://www.popuptest.com/" );
    }

    void gwBrowser_DocumentTitleChanged( object sender, EventArgs e )
    {
        tabPageMain.Text = gwBrowser.DocumentTitle;
    }

    void gwBrowser_DocumentCompleted( object sender, EventArgs e )
    {
        // Some work on document completed
    }

    internal nsIWebBrowserChrome AddNewTab()
    {
        GeckoWebBrowser gwBrowserTab = new GeckoWebBrowser();
        gwBrowserTab.DocumentTitleChanged += gwBrowserTab_DocumentTitleChanged;
        gwBrowserTab.DocumentCompleted += gwBrowserTab_DocumentCompleted;

        TabPage oTabPage = new TabPage( "New Tab" );
        Padding oPadding = oTabPage.Margin; oPadding.All = 0;
        oPadding = oTabPage.Padding; oPadding.All = 0;
        oTabPage.Controls.Add( gwBrowserTab );

        gwBrowserTab.Dock = DockStyle.Fill;

        tabControlMain.TabPages.Add( oTabPage );

        return gwBrowserTab;
    }

    private void gwBrowserTab_DocumentTitleChanged( object sender, EventArgs e )
    {
        GeckoWebBrowser gwBrowserTab = sender as GeckoWebBrowser;
        if ( gwBrowserTab == null ) return;

        TabPage oTabPage = gwBrowserTab.Parent as TabPage;
        if ( oTabPage == null ) return;

        oTabPage.Text = gwBrowserTab.DocumentTitle;
    }

    private void gwBrowserTab_DocumentCompleted( object sender, EventArgs e )
    {
        GeckoWebBrowser gwBrowserTab = sender as GeckoWebBrowser;
        if ( gwBrowserTab == null ) return;

        TabPage oTabPage = gwBrowserTab.Parent as TabPage;
        if ( oTabPage == null ) return;

        tabControlMain.SelectedTab = oTabPage;
    }
}

关键的是,自定义窗口创建者:

public sealed class TabbedBrowserCreator: nsIWindowCreator2
{
    private MainForm m_Parent;

    public TabbedBrowserCreator( MainForm p_Parent )
    {
        m_Parent = p_Parent;
    }

    nsIWebBrowserChrome nsIWindowCreator2.CreateChromeWindow( nsIWebBrowserChrome parent, uint chromeFlags )
    {
        bool bCancel = false;
        return CreateWindow( parent, chromeFlags, 0, null, ref bCancel );
    }

    nsIWebBrowserChrome nsIWindowCreator2.CreateChromeWindow2( nsIWebBrowserChrome parent, uint chromeFlags, uint contextFlags, nsIURI uri, ref bool cancel )
    {
        return CreateWindow( parent, chromeFlags, contextFlags, uri, ref cancel );
    }

    nsIWebBrowserChrome nsIWindowCreator.CreateChromeWindow( nsIWebBrowserChrome parent, uint chromeFlags )
    {

        bool cancel = false;
        return CreateWindow( parent, chromeFlags, 0, null, ref cancel );
    }

    private nsIWebBrowserChrome CreateWindow( nsIWebBrowserChrome parent, uint chromeFlags, uint contextFlags, nsIURI uri, ref bool cancel )
    {
        GeckoWindowFlags flags = (GeckoWindowFlags)chromeFlags;

        if ( (flags & GeckoWindowFlags.OpenAsChrome) != 0 )
        {
            // create the child window
            nsIXULWindow xulChild = AppShellService.CreateTopLevelWindow( null, null, chromeFlags, -1, -1 );

            // this little gem allows the GeckoWebBrowser to be properly activated when it gains the focus again
            if ( parent is GeckoWebBrowser && (flags & GeckoWindowFlags.OpenAsDialog) != 0 )
            {
                EventHandler gotFocus = null;
                gotFocus = delegate( object sender, EventArgs e )
                {
                    var br = (sender as GeckoWebBrowser);
                    if ( br != null )
                    {
                        br.GotFocus -= gotFocus;
                        br.WebBrowserFocus.Activate();
                    }

                };
                var parBr = parent as GeckoWebBrowser;
                parBr.GotFocus += gotFocus;
            }

            // return the chrome
            return Xpcom.QueryInterface<nsIWebBrowserChrome>( xulChild );
        }

        nsIWebBrowserChrome oBrowser = m_Parent.AddNewTab();
        oBrowser.SetChromeFlagsAttribute( chromeFlags );
        return oBrowser;
    }
}

希望这会有所帮助。 干杯