按下键时如何在鼠标位置设置 HtmlElement 的样式

How to set the Style of a HtmlElement at the Mouse position when a Key is pressed

我在 WebBrowser 控件的当前文档中有一个 TABLE 元素。
当我按下 A

时,我希望能够在光标位于单元格上时为其着色
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        htmlDocument1 = WebBrowser1.Document
        AddHandler htmlDocument1.MouseOver, AddressOf Me.gettd
End Sub

Public Sub gettd(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
    Dim theElementCollection As HtmlElementCollection
    theElementCollection = WebBrowser1.Document.GetElementsByTagName("td")
    For Each curElement As HtmlElement In theElementCollection
        e.ToElement.Style = "background-color: orange;"
    Next
End Sub

设置KeyPreview = True

Public Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
   If e.KeyCode = Keys.A Then
   ...
   end if
End Sub

我还能如何实现这一目标?

需要进行一些更改:

  • DocumentCompleted 事件可用于向其他事件添加处理程序,但您必须记住,此事件会多次引发 每个 URI 导航:您正在向 MouseOver 事件添加大量处理程序。
    在这种情况下,当 URI 已 Navigated and remove it when we're Navigating 到另一个时添加处理程序更简单。
    另外,请阅读此处的注释:

  • 我们可以简单地使用 MouseOverMouseMove 事件提供的 Document.GetElementFromPoint() method, passing the e.ClientMousePosition 值作为当前点位置。

  • 窗体的 KeyDown 事件不能用于捕获按下的键 - 即使使用 KeyPreview set to True - when other controls capture the input. You can get that key press overriding the Form's ProcessCmdKey,因为它是在预处理消息时调用的。

  • 在访问 HtmlElement 或其属性之前始终检查 null。例如,从未初始化的字符串属性是 null (Nothing),而不是 String.Empty.

将此代码添加到表单,然后导航到 Html 页面:

webBrowser1.Navigate("[Some URI]")

我添加了一个切换功能,在按下 Keys.A 时将当前单元格的样式设置为 background-color 值,并在再次按下时将其设置回原来的值。这里,硬编码为 white,但如果需要,您可以添加一些逻辑来保存以前的样式。

Public Class FormBrowser
    Private trackedElement As HtmlElement = Nothing
    Private elementColorOrange As String = "background-color: orange;"
    Private elementColorWhite As String = "background-color: white;"

    Private Sub webBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles webBrowser1.Navigating
        If webBrowser1.Document Is Nothing Then Return
        RemoveHandler webBrowser1.Document.MouseOver, AddressOf OnBrowserMouseOver
    End Sub

    Private Sub webBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles webBrowser1.Navigated
        AddHandler webBrowser1.Document.MouseOver, AddressOf OnBrowserMouseOver
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
        If keyData = Keys.A Then
            If trackedElement IsNot Nothing AndAlso trackedElement.TagName = "TD" Then
                Dim currentStyle As String = trackedElement.Style & ""
                trackedElement.Style = If(currentStyle.Contains(elementColorOrange), elementColorWhite, elementColorOrange)
                Return True
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
End Class

Private Sub OnBrowserMouseOver(sender As Object, e As HtmlElementEventArgs)
    Dim doc = DirectCast(sender, WebBrowser).Document
    If doc Is Nothing Then Return
    trackedElement = doc.GetElementFromPoint(e.ClientMousePosition)
    txtElmName.Text = trackedElement?.OuterHtml
    txtElmValue.Text = trackedElement?.InnerHtml
End Sub

这是它的工作原理: