다음은 IMessageFilter를 사용한 빠른 예입니다. 의 IMessageFilter는 응용 프로그램의 모든 형태에서 작동합니다 여기뿐만 아니라 주요 하나 특히 바람직하다 :
Public Class Form1
Private WithEvents IdleDetector As IdleTimeout
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
IdleDetector = New IdleTimeout(New TimeSpan(0, 1, 0)) ' one minute timeout duration
Application.AddMessageFilter(IdleDetector)
IdleDetector.Reset()
End Sub
Private Sub IdleDetector_UserIsIdle() Handles IdleDetector.UserIsIdle
' ... do something in here ...
MessageBox.Show("Inactivity detected...locking some feature!")
Button1.Enabled = False
' restart the timeout period?
' IdleDetector.Reset()
End Sub
End Class
Public Class IdleTimeout
Implements IMessageFilter
Public Event UserIsIdle()
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_LBUTTONDBLCLK As Integer = &H203
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Const WM_RBUTTONDBLCLK As Integer = &H206
Private Const WM_MBUTTONDOWN As Integer = &H207
Private Const WM_MBUTTONUP As Integer = &H208
Private Const WM_MBUTTONDBLCLK As Integer = &H209
Private Const WM_MOUSEWHEEL As Integer = &H20A
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105
Private IdleTimeoutDuration As TimeSpan
Private WithEvents tmr As System.Timers.Timer
Private TargetDateTime As DateTime
Private SC As WindowsFormsSynchronizationContext
Public Sub New(ByVal TimeoutDuration As TimeSpan)
SC = System.Windows.Forms.WindowsFormsSynchronizationContext.Current
Me.IdleTimeoutDuration = TimeoutDuration
tmr = New System.Timers.Timer
tmr.Interval = 1000
Me.Reset()
End Sub
Public Sub Reset()
TargetDateTime = DateTime.Now.Add(IdleTimeoutDuration)
tmr.Start()
End Sub
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
Select Case m.Msg
Case WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP, _
WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, _
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK, _
WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MBUTTONDBLCLK
TargetDateTime = DateTime.Now.Add(IdleTimeoutDuration)
End Select
Return False
End Function
Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
If TargetDateTime.Subtract(DateTime.Now).TotalMilliseconds < 0 Then
tmr.Stop()
If Not IsNothing(SC) Then
SC.Post(New System.Threading.SendOrPostCallback(AddressOf RaiseIdleEvent), Nothing)
End If
Else
' This is a one second timer so you could raise a "time out duration left" type event if you wanted to ...
End If
End Sub
Private Sub RaiseIdleEvent(ByVal x As Object)
' ... do not call directly ...
' Called via SC.Post() which puts this in the main UI thread!
RaiseEvent UserIsIdle()
End Sub
End Class
우리가 마우스 클릭을 얻었다 후 - 우리가 주요 활성화해야 창을 먼저 누른 다음 .Focus()를 호출하면 팝업 내 컨트롤의 컨트롤이 예상대로 작동합니다. – user2526236
다음을 확인하십시오 : http://stackoverflow.com/a/20434431/897326 질문에 답변이 있습니까? – Neolisk
IMessageFilter를 사용자 지정 클래스에 구현하고 WndProc()에 원하는 "작업"을 모두 트랩합니다. 제한 시간 내에 활동이 감지되지 않으면 해당 클래스에서 맞춤 이벤트를 발생시킵니다. –