在 WebBrowser 控件的新选项卡中打开新网页

Open new web page in new tab in WebBrowser control(在 WebBrowser 控件的新选项卡中打开新网页)

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

问题描述

我在我的 c# 应用程序中使用 WebBrowser 控件,我想在其中打开网页.
完全搞定了.

I'm using WebBrowser control in my c# application, and I want to open web pages in it.
It's completely done.

我的问题:

网页中目标为_blank的任何链接都将在新的IE窗口中打开.

Any link in the web page that its target is _blank will open in new IE Window.

我想在我的应用程序的新标签中打开此类链接.

I'd like to open such link in new tab in my application.

怎么做?

谢谢!

推荐答案

private void Browser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
    var webBrowser = (WebBrowser)sender;
    if (webBrowser.Document != null)
    {
        foreach (HtmlElement tag in webBrowser.Document.All)
        {
            if (tag.Id == null)
            {
                tag.Id = String.Empty;
                switch (tag.TagName.ToUpper())
                {
                    case "A":
                    {
                        tag.MouseUp += new HtmlElementEventHandler(link_MouseUp);
                        break;
                    }
                }
            }
        }
    }
}


private void link_MouseUp(object sender, HtmlElementEventArgs e)
{
    var link = (HtmlElement)sender;
    mshtml.HTMLAnchorElementClass a = (mshtml.HTMLAnchorElementClass)link.DomElement;
    switch (e.MouseButtonsPressed)
    {
        case MouseButtons.Left:
        {
            if ((a.target != null && a.target.ToLower() == "_blank") || e.ShiftKeyPressed || e.MouseButtonsPressed == MouseButtons.Middle)
            {
                AddTab(a.href);
            }
            else
            {
                CurrentBrowser.TryNavigate(a.href);
            }
            break;
        }
        case MouseButtons.Right:
        {
            CurrentBrowser.ContextMenuStrip = null;
            var contextTag = new ContextTag();
            contextTag.Element = a;
            contextHtmlLink.Tag = contextTag;
            contextHtmlLink.Show(Cursor.Position);
            break;
        }
    }
}

在 dotBrowser.sf.net 项目中查看更多信息

See more at dotBrowser.sf.net project

这篇关于在 WebBrowser 控件的新选项卡中打开新网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 WebBrowser 控件的新选项卡中打开新网页

基础教程推荐