C#(WinForm) WebBrowser 控件, 怎样获取鼠标点击处的 URL???
如题 --------------------编程问答-------------------- BeforeNavigate2事件中的URL参数 --------------------编程问答-------------------- 不行,BeforeNavigate2 —— 找不到这事件!!1 --------------------编程问答-------------------- 试试Navigating事件 WebBrowserNavigatingEventArgs.Url --------------------编程问答-------------------- 也不行,要改变 URL 时,Navigating 事件才会发生。没获得新的 URL ,Navigating 不响应。 --------------------编程问答-------------------- 引用microsoft html object
private void WebBrowser1_DocumentCompleted(object sender , System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e )
{
foreach(HtmlElement link in Me.WebBrowser1.Document.All)
{
if(link.DomElement.GetType() == typeof(mshtml.HTMLAnchorElementClass)
{
((mshtml.HTMLAnchorElementClass)link.DomElement).HTMLAnchorEvents2_Event_onclick += new HTMLAnchorEvents2_onclickEventHandler(link_HTMLAnchorEvents2_Event_onclick);
}
}
}
ptivate bool link_HTMLAnchorEvents2_Event_onclick(mshtml.IHTMLEventObj pEvtObj)
{
MessageBox.Show(((mshtml.HTMLAnchorElementClass)pEvtObj.srcElement).href);
} --------------------编程问答-------------------- up --------------------编程问答-------------------- 先定义一个 dom,然后把 <a> 元素取出来,添加你想要的 超链接一个 click事件。
可以试试如下代码:
Private Sub AddLinksEvent()
Dim doc As HtmlDocument = Me.WebBrowser1.Document
Dim els As HtmlElementCollection = doc.GetElementsByTagName("a")
For Each el As HtmlElement In els
If String.Compare(el.InnerText, "Prepare Reply", True) = 0 Then
AddHandler el.Click, AddressOf Element_Click
End If
Next
End Sub
Private Sub Element_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
Dim el As HtmlElement
el = Me.WebBrowser1.Document.GetElementFromPoint(e.MousePosition)
'Dim sPn As String = GetPN(el)
End Sub
--------------------编程问答--------------------
Browser.Document.Click += new HtmlElementEventHandler(Document_Click);
void Document_Click(object sender, HtmlElementEventArgs e)
{
HtmlElement _HtmlElement = ((HtmlDocument)sender).ActiveElement;
string URL = _HtmlElement.GetAttribute("href");
Browser.Navigate(new Uri(URL));
}
--------------------编程问答-------------------- 你大哥我来帮你了,我找到一个例子,你可以参考一下,很不错的。
C# 复制代码
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText =
"<html><body>Please enter your name:<br/>" +
"<input type='text' name='userName'/><br/>" +
"<a href='http://www.microsoft.com'>continue</a>" +
"</body></html>";
webBrowser1.Navigating +=
new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}
private void webBrowser1_Navigating(object sender,
WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document =
this.webBrowser1.Document;
if (document != null && document.All["userName"] != null &&
String.IsNullOrEmpty(
document.All["userName"].GetAttribute("value")))
{
e.Cancel = true;
System.Windows.Forms.MessageBox.Show(
"You must enter your name before you can navigate to " +
e.Url.ToString());
}
} --------------------编程问答-------------------- http://www.hdhacker.com/?fromuid=11672
补充:.NET技术 , C#