Public Class Form1
Private WithEvents mf As New MyFilter
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Application.AddMessageFilter(mf)
End Sub
Private Sub mf_Inactivity() Handles mf.Inactivity
Me.WindowState = FormWindowState.Minimized
End Sub
Private Class MyFilter
Implements IMessageFilter
Public Event Inactivity()
Private dtTarget As DateTime
' Private Const WM_MOUSEMOVE As Integer = &H200 ' UNRELIABLE INDICATOR
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 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 WithEvents tmr As New Timer
Public Sub New()
tmr.Interval = 1000
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_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP, _
WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, _
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK
dtTarget = DateTime.Now.AddSeconds(10)
tmr.Start()
End Select
End Function
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
If DateTime.Now > dtTarget Then
tmr.Stop()
RaiseEvent Inactivity()
End If
End Sub
End Class
End Class
|