C# Web 浏览器导航

发布于 2024-11-06 04:15:14 字数 529 浏览 1 评论 0原文

我在我的应用程序上打开了浏览器,使用默认的网络浏览器加载数据。

//Set the function and display the browsers we're using (per screen)
browsers[index].Width = screens[index].Bounds.Width;
browsers[index].Height = screens[index].Bounds.Height;
browsers[index].Location = new System.Drawing.Point(screens[index].Bounds.X, screens[index].Bounds.Y);

browsers[index].Navigate(new Uri(lines[index]));
browsers[index].Show();

现在我的问题是,当您单击页面上的链接时,它会离开我的应用程序并完全打开一个新的浏览器。有办法摆脱这个吗?

lines 包含 URL 数组,browser 是要在不同屏幕上加载的网页数组。

I have open browsers on my application which load in data using the default web browser.

//Set the function and display the browsers we're using (per screen)
browsers[index].Width = screens[index].Bounds.Width;
browsers[index].Height = screens[index].Bounds.Height;
browsers[index].Location = new System.Drawing.Point(screens[index].Bounds.X, screens[index].Bounds.Y);

browsers[index].Navigate(new Uri(lines[index]));
browsers[index].Show();

Now my problem is that when you click a link on the page, it leaves my application and openes a new browser completely. Any way of getting out of this?

lines contains an array of URL's and browsers is an array of web pages to load up on different screens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-11-13 04:15:14

如果我没记错的话,这是因为 'TARGET = "_blank"' 发生的,我会尝试在渲染内容之前从标签中删除它。

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;
        }
    }
}

来源:http://stackoverflow.com/questions/5312275/open-new-web-page-in-new-tab-in-webbrowser-control

If I am not wrong this is happening because of 'TARGET = "_blank"' , I would try removing this this from the tag before the content is rendered.

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;
        }
    }
}

Source:http://stackoverflow.com/questions/5312275/open-new-web-page-in-new-tab-in-webbrowser-control

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文