【问题标题】:MouseMove Not Recognizing PositionMouseMove 无法识别位置
【发布时间】:2015-02-01 17:52:03
【问题描述】:

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

    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,但没用。

希望这是有道理的。

【问题讨论】:

  • 这是完全正常的,那些其他控件会获取 MouseMove 事件。您必须将 Capture 属性设置为 True 以避免发生这种情况。不是真正有用的东西,这不是需要解决的问题。

标签: vb.net mousemove


【解决方案1】:

您可以使用单一方法为父 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 方法,第一个方法就会按您的意愿工作。

【讨论】:

  • 嗨 jmcilhinney,如果可以的话,还有一个问题。似乎其他控件现在继承了 tablelayout 事件。例如,如果光标在距表格布局顶部 5px 处更改,则光标也会在距其他控件顶部 5px 处更改,至少在moemt 发生的情况是这样。你知道解决这个问题的方法吗?
  • 嗯,我也许可以使用 if 语句来识别控件更改。目前不知道该怎么做,但如果可能的话,我应该能够弄清楚。
  • 对不起,忽略我,它工作得很好。我忘了修改我的代码以读取 location.x 而不是 e.location.x。
猜你喜欢
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多