如果表单上没有操作,如何在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
但出于某种原因,第一次它运作正常,下次它会激活很多次。
答案 0 :(得分:1)
您可以使用IMessageFilter和Application.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
答案 1 :(得分:0)
我认为你正在每毫秒创建一个新的处理程序,鼠标处于运动状态。我认为您希望在表单加载时创建计时器,然后在每次鼠标移动时将其重置为0。
答案 2 :(得分:0)
另一个选项与此帖https://stackoverflow.com/a/44552767/5162073类似。您可以在任何空闲时间后根据需要进行响应。
If a.GetIdleTime() > 300 Then
showLogin()
End If