在新标签页中打开链接(WebBrowser控件) [英] Open link in new TAB (WebBrowser Control)

查看:1423
本文介绍了在新标签页中打开链接(WebBrowser控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何点击一个链接在WebBrowser控件在WinForms应用程序,然后让该链接在新标签中打开我的TabControl里面?

Does anybody know how to click on a link in the WebBrowser control in a WinForms application and then have that link open in a new tab inside my TabControl?

我一直在寻找了几个月,看到很多教程/文章/ code样品,但它好像从来没有人尝试过​​这在C#中之前。

I've been searching for months, seen many tutorials/articles/code samples but it seems as though nobody has ever tried this in C# before.

任何意见/样品是极大的AP preciated。

Any advice/samples are greatly appreciated.

感谢你。

推荐答案

根据您的意见,我知道你想陷阱在新窗口打开行动WebBrowser控件,并覆盖默认行为,以打开您的应用程序,而不是内部的一个新的标签。

Based on your comments, I understand that you want to trap the "Open In New Window" action for the WebBrowser control, and override the default behavior to open in a new tab inside your application instead.

要可靠地做到这一点,你需要获得在NewWindow2事件,它暴露ppDisp(可设置指针的WebBrowser控件应该打开新窗口)。 所有其他潜在黑客一起解决方案(例如获得下的openWindow事件之前的用户选择的最后链接)不是最佳,并结合在拐角情​​况失败。

To accomplish this reliably, you need to get at the NewWindow2 event, which exposes ppDisp (a settable pointer to the WebBrowser control that should open the new window). All of the other potential hacked together solutions (such as obtaining the last link selected by the user before the OpenWindow event) are not optimal and are bound to fail in corner cases.

幸运的是,实现这一点,同时还使用System.Windows.Forms.WebBrowser控件为基础的(相对)简单的方法。所有你需要做的是延长了web浏览器和拦截NewWindow2事件,同时向公众提供该ActiveX实例(传递到ppDisp在新标签页)。这样做是之前,和毛里西奥·罗哈斯有一个很好的例子,有一个完整的工人阶级ExtendedWebBrowser:

Luckily, there is a (relatively) simple way of accomplishing this while still using the System.Windows.Forms.WebBrowser control as a base. All you need to do is extend the WebBrowser and intercept the NewWindow2 event while providing public access to the ActiveX Instance (for passing into ppDisp in new tabs). This has been done before, and Mauricio Rojas has an excellent example with a complete working class "ExtendedWebBrowser":

<一个href="http://blogs.artinsoft.net/mrojas/archive/2008/09/18/newwindow2-events-in-the-c-webbrowsercontrol.aspx">http://blogs.artinsoft.net/mrojas/archive/2008/09/18/newwindow2-events-in-the-c-webbrowsercontrol.aspx

一旦你有了ExtendedWebBrowser类,所有你需要做的是建立处理程序NewWindow2和点ppDisp在新的选项卡浏览器。下面是我把一个例子:

Once you have the ExtendedWebBrowser class, all you need to do is setup handlers for NewWindow2 and point ppDisp to a browser in a new tab. Here's an example that I put together:

    private void InitializeBrowserEvents(ExtendedWebBrowser SourceBrowser)
    {
        SourceBrowser.NewWindow2 += new EventHandler<NewWindow2EventArgs>(SourceBrowser_NewWindow2);
    }

    void SourceBrowser_NewWindow2(object sender, NewWindow2EventArgs e)
    {

        TabPage NewTabPage = new TabPage()
        {
            Text = "Loading..."
        };

        ExtendedWebBrowser NewTabBrowser = new ExtendedWebBrowser()
        {
            Parent = NewTabPage,
            Dock = DockStyle.Fill,
            Tag = NewTabPage
        };

        e.PPDisp = NewTabBrowser.Application;
        InitializeBrowserEvents(NewTabBrowser);

        Tabs.TabPages.Add(NewTabPage);
        Tabs.SelectedTab = NewTabPage;

    }

    private void Form1_Load(object sender, EventArgs e)
    {

        InitializeBrowserEvents(InitialTabBrowser);

    }

(假设的TabControl命名为标签和初始含子标签控件停靠ExtendedWebBrowser名为InitialWebBrowser)

(Assumes TabControl named "Tabs" and initial tab containing child control docked ExtendedWebBrowser named "InitialWebBrowser")

不要忘记注销事件时,标签被关闭!

Don't forget to unregister the events when the tabs are closed!

这篇关于在新标签页中打开链接(WebBrowser控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆