簡體   English   中英

C#Web瀏覽器導航

[英]C# Web browser navigation

我的應用程序上有打開的瀏覽器,這些瀏覽器使用默認的Web瀏覽器加載數據。

//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();

現在我的問題是,當您單擊頁面上的鏈接時,它將離開我的應用程序並完全打開新的瀏覽器。 有什么辦法擺脫這種情況嗎?

每行包含一個URL數組,瀏覽器是一組要在不同屏幕上加載的網頁。

如果我沒有記錯,這是由於'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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM