WinForms WebBrowser控件:强制所有链接在新(IE)窗口中外部打开
-
03-07-2019 - |
题
我认为标题涵盖了所有内容。我使用控件来显示一些带有一些标记的基本HTML,并且可能还有链接。我想要做的是强制点击任何链接以在新的IE窗口中打开此链接,而不是导航到控件本身的那个页面。
有什么想法吗?
解决方案
您可以处理Navigating事件,将WebBrowserNavigatingEventArgs的Cancel属性设置为true,并使用Process.Start在IE中打开URL。
这样的事情:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// prevents WebBrowser to navigate
if (e.Url.Host.Length > 0) // Otherwise the default about:blank when you init the control doesn't work
{
e.Cancel = true;
// Open the URL in an IE window
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = e.Url.ToString();
process.Start();
}
}
不隶属于 StackOverflow