鼠标移动无法识别位置



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

    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

我想知道的是,是否有不同的鼠标移动事件只会集中在光标位置上,而不是它的移动位置。我尝试过表单鼠标移动,但这不起作用。

希望这是有道理的。

您可以使用

单个方法处理父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

只需在将子控件添加到表时调用该 secoind 方法,然后第一个方法将按所需工作。

最新更新