locked
WPF WebBrowser Control on x64 machines Launches new IE window instead of staying inside of control. RRS feed

  • Question

  • I have a WPF app that uses an embedded WebBrowser control.  I have been using this as a video player (i would have used MediaPlayer, but it doesn't support H.264 in XP)  Anyway, I am using a 3rd party video player to display the h.264 videos, and this has been working fine.

    We started testing on 64 bit Windows 7 and now whenever we attempt to play the videos, the WebBrowser launches a separate IE window and plays the video there instead of within my app.

    I have googled this bug and have seen hints about handling the NewWindow2 event, but have yet to see a decent example that isn't a total HACK..

    Does anybody know a way to stop the WebBrowser control from launching separate IE windows on a 64 bit OS?

    Thanks a lot,

    Curtis

    Monday, September 27, 2010 8:31 PM

Answers

  • Hi Curtus,

    Based on my understand, you used the WPF WebControl and host a Web page to play the video. And a "3rd party video player", dose it request to play the video in a new window? I am not sure if it has the script "window.open()" to open a new window in the web page.

    As far as I know, the WebBrower control opens a URL in a new window, it is an internal operation (different version may have different behaviour, XP uses the IE 6 default, and Win 7 uses IE 8). The IE-based WebBrowser actually exposes a NewWindow2, NewWindow3 events on its COM interface. Unfortunately, the WPF WebBrowser control doesn't implement this event. Otherwise you can just put some code in that event handler and stop this behaviour to open a new browser instacne. The WinForm version of the WebBrowser control provided the corresponding NewWindow event as the blog from drLich's link. However, we can't get the target URL from the event arguments, so it is still not enough to implement a workaround. We need to host the ActiveX version of the WebBrowser control ourselves, and use the NewWindow3 event mentioned above.

    And add a reference to the SHDocVw (Microsoft Internet Controls).

    XAML:

     <Grid>
     <WindowsFormsHost Name="windowsFormsHost1">
      <wf:WebBrowser x:Name="webBrowser" Url="..."/>
     </WindowsFormsHost>
     </Grid>
    

    C#:

     public MainWindow()
     {
     InitializeComponent();
     ((SHDocVw.DWebBrowserEvents2_Event)browser.ActiveXInstance).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(MainWindow_NewWindow3);
     }
    
     void MainWindow_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
     {
     browser.Navigate(bstrUrl);
     Cancel = true;
     }
    

    Please note: if you develop it in Visual Studio 2010 with .Net 4, please set the properties "Embed Interop Type" of the COM interface - "Interop.SHDocVw" to False and set the "Local Copy" to True. Otherwise, the COM cannot be public the NewWindow3 for managed code. For more about Embed Interop Type in .Net 4, please refer to: http://msdn.microsoft.com/en-us/library/dd997297.aspx

    Sincerely,
    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    • Marked as answer by Jie Bao Friday, October 8, 2010 11:18 AM
    Wednesday, September 29, 2010 7:19 AM

All replies

  • Monday, September 27, 2010 8:59 PM
  • Hi Curtus,

    Based on my understand, you used the WPF WebControl and host a Web page to play the video. And a "3rd party video player", dose it request to play the video in a new window? I am not sure if it has the script "window.open()" to open a new window in the web page.

    As far as I know, the WebBrower control opens a URL in a new window, it is an internal operation (different version may have different behaviour, XP uses the IE 6 default, and Win 7 uses IE 8). The IE-based WebBrowser actually exposes a NewWindow2, NewWindow3 events on its COM interface. Unfortunately, the WPF WebBrowser control doesn't implement this event. Otherwise you can just put some code in that event handler and stop this behaviour to open a new browser instacne. The WinForm version of the WebBrowser control provided the corresponding NewWindow event as the blog from drLich's link. However, we can't get the target URL from the event arguments, so it is still not enough to implement a workaround. We need to host the ActiveX version of the WebBrowser control ourselves, and use the NewWindow3 event mentioned above.

    And add a reference to the SHDocVw (Microsoft Internet Controls).

    XAML:

     <Grid>
     <WindowsFormsHost Name="windowsFormsHost1">
      <wf:WebBrowser x:Name="webBrowser" Url="..."/>
     </WindowsFormsHost>
     </Grid>
    

    C#:

     public MainWindow()
     {
     InitializeComponent();
     ((SHDocVw.DWebBrowserEvents2_Event)browser.ActiveXInstance).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(MainWindow_NewWindow3);
     }
    
     void MainWindow_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
     {
     browser.Navigate(bstrUrl);
     Cancel = true;
     }
    

    Please note: if you develop it in Visual Studio 2010 with .Net 4, please set the properties "Embed Interop Type" of the COM interface - "Interop.SHDocVw" to False and set the "Local Copy" to True. Otherwise, the COM cannot be public the NewWindow3 for managed code. For more about Embed Interop Type in .Net 4, please refer to: http://msdn.microsoft.com/en-us/library/dd997297.aspx

    Sincerely,
    Bob Bao

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    • Marked as answer by Jie Bao Friday, October 8, 2010 11:18 AM
    Wednesday, September 29, 2010 7:19 AM
  • This helped me a lot!

    Using VS 2013 with .NET 4.5 I found out that the NewWindow3 event would never happen. But the DWebBrowserEvents_NewWindowEventHandler gives you a URL as parameter that is obviously the navigation target. One might use this for an in-window navigation and set parameter Processed = true in order to cancel the opening of an external window.


    Tuesday, February 23, 2016 1:05 PM