WebBrowser-Control - open default browser on click on link

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

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

    

Now, when I navigat

3条回答
  •  心在旅途
    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.

提交回复
热议问题