2017-06-19 26 views
1

如果在表单上没有任何操作,如何激发vb.net中的事件/运行功能?如果在表单上没有任何操作,运行函数

我已经试过这一点:

Private Sub Window_MouseMove(sender As Object, e As MouseEventArgs) 

    mint_LastInitializedTimerID = mint_LastInitializedTimerID + 0.00000001 


    Dim intMilliseconds As Integer = 5000 


    Dim objTimer As New System.Timers.Timer(intMilliseconds) 
    AddHandler objTimer.Elapsed, AddressOf Window_TimerElapsed 

    objTimer.AutoReset = False 
    objTimer.Enabled = True 

End Sub 
Private Sub Window_TimerElapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) 

    mint_LastReceivedTimerID = mint_LastReceivedTimerID + 0.00000001 


    If mint_LastReceivedTimerID = mint_LastInitializedTimerID Then 
     Me.Dispatcher.Invoke(Sub() showLogin(), System.Windows.Threading.DispatcherPriority.Normal) 
    End If 

End Sub 

Public Function showLogin() 
    WinUIMessageBox.Show(Window.GetWindow(Me), "!", "...", CType("1", MessageBoxButton), MessageBoxResult.None, MessageBoxOptions.None) 

End Function 

但由于某些原因,第一次它的工作原理确定,下一次它触发了次功能很多。

回答

1

您可以使用IMessageFilterApplication.AddMessageFilter以确定当用户键入或使用鼠标任何地方在您的应用程序。让您的自定义过滤器类提高自定义事件您的主窗体(或其他)陷阱。快速举例...

Public Class Form1 

    Private WithEvents mmf As MyMessageFilter 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     mmf = New MyMessageFilter(TimeSpan.FromSeconds(5)) 
     Application.AddMessageFilter(mmf) 
    End Sub 

    Private Sub mmf_UserIdle() Handles mmf.UserIdle 
     Me.Text = "User Idle @ " & DateTime.Now 
    End Sub 

    Private Class MyMessageFilter 
     Implements IMessageFilter 

     Private Enum UserActivity 
      WM_KEYDOWN = &H100 
      WM_KEYUP = &H101 
      WM_SYSKEYDOWN = &H104 
      WM_SYSKEYUP = &H105 
      WM_MOUSEMOVE = &H200 
      WM_LBUTTONDOWN = &H201 
      WM_LBUTTONUP = &H202 
      WM_RBUTTONDOWN = &H204 
      WM_RBUTTONUP = &H205 
     End Enum 

     Public Event UserIdle() 

     Private WithEvents tmr As New System.Timers.Timer() 
     Private SC As System.Threading.SynchronizationContext 

     Private Sub New() 
     End Sub 

     Public Sub New(ByVal TimeOutDuration As TimeSpan) 
      SC = System.Windows.Forms.WindowsFormsSynchronizationContext.Current 
      tmr.Interval = TimeOutDuration.TotalMilliseconds 
      tmr.Start() 
     End Sub 

     Private Sub Reset() 
      tmr.Stop() 
      tmr.Start() 
     End Sub 

     Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed 
      ' raise the event in a thread safe manner 
      If Not IsNothing(SC) Then 
       SC.Post(New System.Threading.SendOrPostCallback(AddressOf GuiSafeRaiseEvent), Nothing) 
      End If 
     End Sub 

     Private Sub GuiSafeRaiseEvent() ' do not call me directly! 
      RaiseEvent UserIdle() 
     End Sub 

     Private Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage 
      Select Case m.Msg 
       Case UserActivity.WM_KEYDOWN, UserActivity.WM_KEYUP, UserActivity.WM_LBUTTONDOWN, UserActivity.WM_LBUTTONUP, UserActivity.WM_MOUSEMOVE, UserActivity.WM_RBUTTONDOWN, UserActivity.WM_RBUTTONUP, UserActivity.WM_SYSKEYDOWN, UserActivity.WM_SYSKEYUP 
        Me.Reset() ' the user did something, reset the timer 

      End Select 

      Return False ' allow normal processing to occur for all messages 
     End Function 

    End Class 

End Class 
0

我想你正在创造一个新的处理程序,鼠标在运动的每毫秒。我想你会想在表单加载时创建计时器,然后在每次鼠标移动时将其重置为0。

相关问题