Share via


Preventing External links from Opening in New Window in WPF Web Browser

Windows Forms Solution:

Developers who had used the traditional web browser control in Windows Forms had an Event called WebBrowser.NewWindow which they can subscribe too when a new window is being opened through a Web Browser control. By using this event we can prevent the new window from opening in Windows Forms WebBrowser Control. 

Here is a code for Windows Forms Web Browser Control to prevent New Window

private string  url = “”;
  
public SimpleBrowser()
{
      this.InitializeComponent();
      WinFormBrowser.DocumentCompleted += new        WebBrowserDocumentCompletedEventHandler(WinFormBrowser_DocumentCompleted);
       WinFormBrowser.NewWindow += new  System.ComponentModel.CancelEventHandler(WinFormBrowser_NewWindow);
}
   
void WinFormBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
      HtmlElementCollection links = WinFormBrowser.Document.Links;
      foreach (HtmlElement var in links)
      {
         var.AttachEventHandler(“onclick”, LinkClicked);
      }
}
  
  
private void  LinkClicked(object  sender, EventArgs e)
{
      HtmlElement link = WinFormBrowser.Document.ActiveElement;
      url = link.GetAttribute("href");
}
  
void WinFormBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
{
      e.Cancel = true;
      WinFormBrowser.Navigate(url);
}

**
WPF Solution:**

In WPF, WebBrowser control doesn’t have an event named NewWindow. So we can’t directly prevent the external window opening in WPF.

We need to get the IWebBrowser2 interface from the WPF WebBrowser control. This allows access to methods on the object that are not publicly exposed for the control. By using this Interface we can subscribe to the NewWindow event and prevent the external window in WPF.

To Implement this

  1.  Add a reference to the COM component named "Microsoft Internet Controls"

              
         
           2.) Use the code below.

<Grid x:Name="LayoutRoot">
   <WebBrowser x:Name="MyWebBrowser"/>
</Grid>
using System.Runtime.InteropServices;
namespace WpfApplication18
{
    public partial  class MainWindow : Window
    {
        static readonly  Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }
 
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            MyWebBrowser.Navigate("provide the url");
            MyWebBrowser.LoadCompleted += MyWebBrowser_LoadCompleted;
        }
 
        void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            IServiceProvider serviceProvider = (IServiceProvider)MyWebBrowser.Document;
            Guid serviceGuid = SID_SWebBrowserApp;
            Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
            SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);
            SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
            wbEvents.NewWindow += new  SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);
        }
 
        void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object  PostData, string  Headers, ref  bool Processed)
        {
            Processed = true;
            MyWebBrowser.Navigate(URL);
        }
 
    }
 
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
    internal interface  IServiceProvider
    {
        [return: MarshalAs(UnmanagedType.IUnknown)]
        object QueryService(ref Guid guidService, ref Guid riid);
    }
}