MouseMove 无法识别位置

MouseMove Not Recognizing Position

我使用以下代码根据光标位置更改光标图像。我注意到,如果光标穿过标签或文本框或其他东西,光标将不会改变,直到它进入我的表格布局的一部分,这可能会改变中间页面。

    Private Sub TableLayoutPanel1_MouseMove(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseMove
    If e.Location.X > Me.Width - 7 And e.Location.Y > 12 And e.Location.Y < Me.Height - 12 Then
        Me.Cursor = Cursors.SizeWE
    ElseIf e.Location.X < 6 And e.Location.Y > 12 And e.Location.Y < Me.Height - 12 Then
        Me.Cursor = Cursors.SizeWE
    ElseIf e.Location.Y > Me.Width - 12 And e.Location.X > 12 And e.Location.X < Me.Width - 12 Then
        Me.Cursor = Cursors.SizeNS
    ElseIf e.Location.Y < 6 And e.Location.X > 12 And e.Location.X < Me.Width - 12 Then
        Me.Cursor = Cursors.SizeNS
    Else
        Me.Cursor = Cursors.Default
    End If
End Sub

我想知道的是,是否有一个不同的 mousemove 事件只关注光标位置而不关注光标移动的位置。我试过 form mousemove,但没用。

希望这是有道理的。

您可以使用单一方法处理父 TableLayoutPanel 及其所有子项的 MouseMove 事件,并根据需要简单地转换位置。

Private Sub TableLayoutPanel1_MouseMove(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseMove
    Dim ctrl = DirectCast(sender, Control)
    Dim location = TableLayoutPanel1.PointToClient(ctrl.PointToScreen(e.Location))

    'location is now a Point relative the the top-left corner of TableLayoutPanel1.
    '...
End Sub

Private Sub HandleMouseMoveForTableChildren()
    For Each child As Control In TableLayoutPanel1.Controls
        RemoveHandler child.MouseMove, AddressOf TableLayoutPanel1_MouseMove
        AddHandler child.MouseMove, AddressOf TableLayoutPanel1_MouseMove
    Next
End Sub

只要在任何时候将子控件添加到 table 时调用该 secoind 方法,第一个方法就会按您的需要工作。