WebBrowser-Control - open default browser on click on link

后端 未结 3 888
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 19:49

I use the WebBrowser-Control in my WPF-Application like

    

Now, when I navigat

相关标签:
3条回答
  • 2021-02-04 20:01

    You can open the new page in default browser using Proces.Start() on Navigating event and set e.Cancel = true; so that the page in the control will not change.

    Example:

    @MainWindow.xaml.cs

    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Navigation;
    
    namespace OpenDefaultBrowser
    {
        public partial class MainWindow : Window
        {
            private static bool willNavigate;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
            {
                // first page needs to be loaded in webBrowser control
                if (!willNavigate)
                {
                    willNavigate = true;
                    return;
                }
    
                // cancel navigation to the clicked link in the webBrowser control
                e.Cancel = true;
    
                var startInfo = new ProcessStartInfo
                {
                    FileName = e.Uri.ToString()
                };
    
                Process.Start(startInfo);
            }
        }
    }
    

    @MainWindow.xaml

    <Window x:Class="OpenDefaultBrowser.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="464" Width="1046">
        <Grid>
            <WebBrowser Height="425" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="1024" Source="http://stackoverflow.com/" Navigating="webBrowser1_Navigating" />
        </Grid>
    </Window>
    
    0 讨论(0)
  • I dont think WebBrowser is intended to be like this as even in normal browser if a hyperlink is clicked and it is represeting a straightforward URL (and not javascript based hyperlink), it will open the URL in that browser window (and that specific tab) itself. WebBrowser control mimics this basic behavior of the browser itself.

    I think you can right click on the hyperlink and say "Open in New Window" (see if that option is enabled in WebBrowser control).

    If that option is disabled you can enable it by using special HTMLHost APIs.

    0 讨论(0)
  • 2021-02-04 20:15

    Web browser control by default does not open default browser while a link is clicked and it opens the link clicked inside the browser in internet explorer only. Now wecan use _DocumentCompleted event but it needs an event based trigger, like link button, to work. Now the issue is if html in the browser control has href then this even will not work. Solution to this is using _NewWindow event. Code is given below

    /* The event handler*/
    private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
            {
                var webbrowser = (WebBrowser)sender;
                e.Cancel = true;
                OpenWebsite(webbrowser.StatusText.ToString());
                webbrowser = null;
            }
    
    /* The function call*/
    public static void OpenWebsite(string url)
            {
                Process.Start(GetDefaultBrowserPath(), url);
            }
    
     private static string GetDefaultBrowserPath()
            {
                string key = @"http\shell\open\command";
                RegistryKey registryKey =
                Registry.ClassesRoot.OpenSubKey(key, false);
                return ((string)registryKey.GetValue(null, null)).Split('"')[1];
            }
    

    Suggestions for improvement are invited. Happy coding.

    0 讨论(0)
提交回复
热议问题