WebBrowser-Control - open default browser on click on link

偶尔善良 提交于 2019-12-03 00:39:43

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>

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.

Mayank Mehta

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!