0
如果有人單擊WebBrowser控件內的超鏈接,我可以使用什麼方法獲取該超鏈接的URL,並檢查它是否具有告知瀏覽器的屬性在新標籤頁/窗口中打開鏈接。 Windows窗體應用程序。獲取Windows窗體應用程序WebBrowser中單擊超鏈接的URL
如果有人單擊WebBrowser控件內的超鏈接,我可以使用什麼方法獲取該超鏈接的URL,並檢查它是否具有告知瀏覽器的屬性在新標籤頁/窗口中打開鏈接。 Windows窗體應用程序。獲取Windows窗體應用程序WebBrowser中單擊超鏈接的URL
您可以使用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);
}
}
}