如何获得鼠标点击处的控件名称或ID
展开全部
添加一个webbrowser,一个textbox并设置允许多行显示,一个panel控件。名称均默认
Public Class Form1
Dim GetObj As Boolean
Dim HtmlObj As HtmlElement
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Web.Navigate(InputBox("请输入网址,例如:;, "导航到", ";))
End Sub
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
GetObj = True
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If GetObj Then
Dim PointFromWeb As Point = Web.PointToClient(MousePosition)
Dim tmp As HtmlElement = Web.Document.GetElementFromPoint(PointFromWeb)
If Not tmp.Equals(HtmlObj) Then
HtmlObj = tmp
Me.Text = HtmlObj.Id & " name: " & HtmlObj.Name
TextBox1.Text = HtmlObj.InnerHtml
End If
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
GetObj = False
End Sub
End Class
用法:
拖拽panel到webbrowser上的控件
Public Class Form1
Dim GetObj As Boolean
Dim HtmlObj As HtmlElement
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Web.Navigate(InputBox("请输入网址,例如:;, "导航到", ";))
End Sub
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
GetObj = True
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If GetObj Then
Dim PointFromWeb As Point = Web.PointToClient(MousePosition)
Dim tmp As HtmlElement = Web.Document.GetElementFromPoint(PointFromWeb)
If Not tmp.Equals(HtmlObj) Then
HtmlObj = tmp
Me.Text = HtmlObj.Id & " name: " & HtmlObj.Name
TextBox1.Text = HtmlObj.InnerHtml
End If
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
GetObj = False
End Sub
End Class
用法:
拖拽panel到webbrowser上的控件
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询