0

如果有人单击 WebBrowser 控件内的超链接,我可以使用什么方法来获取该超链接的 url 并检查它是否具有告诉浏览器在新选项卡/窗口中打开链接的属性。Windows 窗体应用程序。

4

1 回答 1

0

Document.ActiveElement您可以使用该属性获取当前具有用户输入焦点的元素。

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
    e.Cancel = true;

    if (webBrowser1.Document != null)
    {
        HtmlElement currentElement = webBrowser1.Document.ActiveElement;
        if (currentElement != null)
        {
            string targetPath = currentElement.GetAttribute("href");

            //You can perform some logic here to determine if the targetPath conformsto your specification and if so...
            MainForm newWindow = new MainForm();
            newWindow.webBrowser1.Navigate(targetPath);
            newWindow.Show(); 

           //Otherwise
           //webBrowser1.Navigate(targetPath);

        }
    }
}
于 2013-01-21T19:08:28.703 回答