隐藏鼠标指针

时间:2011-05-13 05:48:47

标签: wpf vb.net .net-3.5

我试图在几秒钟不活动时隐藏鼠标指针,然后在用户移动鼠标时再次重新显示指针。我已经能够让鼠标指针隐藏并重新显示,但是当我执行grid.Children.Clear()grid.Children.Add()鼠标指针重新出现时(但是在几秒钟之后再次隐藏)不活动)。

我的代码如下:

Private Sub Window1_MouseMoved(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
  'MsgBox("Mouse Has Moved", MsgBoxStyle.Critical, "Mouse Moved")
  LastMouseMove = DateTime.Now
  If IsHidden Then
    Cursor = Cursors.Arrow
    IsHidden = False
  End If
End Sub

Private Sub MouseHide_Tick(ByVal sender As Object, ByVal e As EventArgs)
  Dim elaped As TimeSpan = DateTime.Now - LastMouseMove
  If elaped >= TimeoutToHide AndAlso Not IsHidden Then
    Cursor = Cursors.None
    IsHidden = True
    'System.Console.SetCursorPosition(0, 0)
  End If
End Sub

Private Sub setupMouseHide()
  Try
    'Dim timer As New System.Timers.Timer(1000)
    Dim dispatcherTimer As DispatcherTimer = New System.Windows.Threading.DispatcherTimer()
    AddHandler dispatcherTimer.Tick, AddressOf MouseHide_Tick
    dispatcherTimer.Interval = New TimeSpan(0, 0, 3)
    dispatcherTimer.Start()
    Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "Setup Display Message: error encountered")
  End Try
End Sub

我想知道这是一个已知问题还是有更好的方法来实现我想要做的事情?

谢谢,

马特

1 个答案:

答案 0 :(得分:1)

这可能是一个错误,但布局更改引起鼠标移动事件的情况并不少见。

我想说你最好的选择是检查并存储鼠标移动事件中鼠标的实际坐标。这样你可以忽略错误的鼠标移动事件。

不理想,但我认为它会起作用。