网站建设技术团队
C# WebBrowser强制使新窗口网页只在WebBrowser打开
  • 日期 : 2012-05-01
private void wB_MainBrowser_NewWindow(object sender, CancelEventArgs e)
         {
             e.Cancel = true;
             try
             {
                 string url = this.wB_MainBrowser.Document.ActiveElement.GetAttribute("href");
 
                 this.wB_MainBrowser.Url = new Uri(url);
             }
             catch
             {
             }
         }




private void SetAllWebItemSelf(HtmlElementCollection items)
         {
             try
             {
                 foreach (HtmlElement item in items)
                 {
                     if (item.TagName.ToLower().Equals("iframe", StringComparison.OrdinalIgnoreCase)==false)
                     {
                         try
                         {
                             item.SetAttribute("target", "_self");
                         }
                         catch
                         { }
                     }
                     else
                     {
                         try
                         {
                             HtmlElementCollection fitems = item.Document.Window.Frames[item.Name].Document.All;
 
                             this.SetAllWebItemSelf(fitems);
                         }
                         catch
                         { }
                     }
                 }
             }
             catch
             {
             }
         }