locked
WebBrowser RRS feed

  • Question

  • I have this Xaml

    <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication4"
            mc:Ignorable="d"
            Title="MainWindow" Height="950" Width="925"
            MouseDown="Window_MouseDown">
        <StackPanel>
            <TextBox x:Name="tb">robolab.by/</TextBox>
            <Button Click="Button_Click">Нажми</Button>
            <WebBrowser x:Name="Web"
                        Height="800"
                        MouseDown="Web_MouseDown"
                        Navigated="Web_Navigated"
                        Navigating="Web_Navigating"></WebBrowser>

        </StackPanel>
    </Window>

    When I go to site and click to reference Vk. Open New Window Internet explorer

    None of the events of the instructions in the WebBrowser does not intercept clicking on this  link

     

    QUESTION

    1) how to intercept Click of this link??

    2) How to block opening Enternet Explorer

    Sunday, October 16, 2016 6:49 AM

Answers

  • You will need to adopt the solution explained in the TechNet article that I linked to. Try this:

    MainWindow.xaml.cs:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Navigation;
    
    namespace WpfApplication4
    {
        public partial class MainWindow : Window
        {
            static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
            {
                IServiceProvider serviceProvider = (IServiceProvider)Web.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;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Web.Navigate("http://www." + tb.Text);
            }
        }
    
    
        [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);
    
        }
    }
    

    MainWindow.xaml:

    <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication4"
            mc:Ignorable="d"
            Title="MainWindow" Height="950" Width="925">
        <StackPanel>
            <TextBox x:Name="tb">robolab.by/</TextBox>
            <Button Click="Button_Click">ClickMe</Button>
            <WebBrowser x:Name="Web"
                        LoadCompleted="MyWebBrowser_LoadCompleted"
                        Height="800"></WebBrowser>
    
        </StackPanel>
    </Window>
    

    Don't forget to add a reference to "Microsoft Internet Controls" from Project->Add Reference->COM->TypeLibaries in Visual Studio.


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Ahlompys Wednesday, October 19, 2016 4:15 PM
    Tuesday, October 18, 2016 6:36 PM

All replies

  • You could handle the Navigating event of the WebBrowser and set the Cancel property to true once the original page has been navigated to:

    <WebBrowser x:Name="Web"
                         Height="800"
                         Source="http://yoursite.com"
                         Navigated="Web_Navigated"></WebBrowser>

            private void Web_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
            {
                Web.Navigating += (ss, ee) => ee.Cancel = true;
            }

    Please also refer to the following TechNet article.

    Preventing External links from Opening in New Window in WPF Web Browser: http://social.technet.microsoft.com/wiki/contents/articles/22943.preventing-external-links-from-opening-in-new-window-in-wpf-web-browser.aspx

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Rashad Rivera Monday, October 17, 2016 10:50 PM
    Monday, October 17, 2016 5:51 PM
  • Goog Day Magnus Thanks you for all answers!!!. You are greate professional. And always help me. Thanks!

    Event Navigating not see when I click for this link:( And open Explorer




    • Edited by Ahlompys Monday, October 17, 2016 6:23 PM
    Monday, October 17, 2016 6:21 PM
  • The Navigating event should get raised when you click on the link. This works for me:

    Make sure that you set the Source and handle the Navigated event as per my previous reply.

    Please provide a reproducible sample of your issue if you need any further help. You can upload a sample project to OneDrive and post the link to it here.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, October 17, 2016 6:28 PM
  • OneDrive  Link OneDriveLink
    • Edited by Ahlompys Monday, October 17, 2016 8:08 PM
    Monday, October 17, 2016 8:07 PM
  • Ahlompys,

    Are you using the NewWindow event link Mangus suggested via the link he provided? 


    - Rashad Rivera www.omegusprime.com

    Monday, October 17, 2016 10:52 PM
  • You will need to adopt the solution explained in the TechNet article that I linked to. Try this:

    MainWindow.xaml.cs:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Navigation;
    
    namespace WpfApplication4
    {
        public partial class MainWindow : Window
        {
            static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
            {
                IServiceProvider serviceProvider = (IServiceProvider)Web.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;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Web.Navigate("http://www." + tb.Text);
            }
        }
    
    
        [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);
    
        }
    }
    

    MainWindow.xaml:

    <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication4"
            mc:Ignorable="d"
            Title="MainWindow" Height="950" Width="925">
        <StackPanel>
            <TextBox x:Name="tb">robolab.by/</TextBox>
            <Button Click="Button_Click">ClickMe</Button>
            <WebBrowser x:Name="Web"
                        LoadCompleted="MyWebBrowser_LoadCompleted"
                        Height="800"></WebBrowser>
    
        </StackPanel>
    </Window>
    

    Don't forget to add a reference to "Microsoft Internet Controls" from Project->Add Reference->COM->TypeLibaries in Visual Studio.


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Ahlompys Wednesday, October 19, 2016 4:15 PM
    Tuesday, October 18, 2016 6:36 PM