在新选项卡(WebBrowser Control)中打开链接 [英] Open link in new TAB (WebBrowser Control)

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

问题描述

有人知道如何在 WinForms 应用程序中单击 WebBrowser 控件中的链接,然后在我的 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?

我已经搜索了几个月,看过很多教程/文章/代码示例,但似乎以前没有人在 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.

非常感谢任何建议/示例.

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 控件作为基础的情况下完成此操作.您需要做的就是扩展 WebBrowser 并拦截 NewWindow2 事件,同时提供对 ActiveX 实例的公共访问(用于在新选项卡中传递到 ppDisp).这个之前已经做过了,Mauricio Rojas 有一个很好的例子,里面有一个完整的工作类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":

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 名为Tabs",初始选项卡包含子控件停靠的 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 Control)中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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