摘要:LIBClassNew.vb - AutoCloseForm
本來是想弄3個class
一個是autologout
一個是autoclose
一個是base
想想看
還是弄成現在這樣好像比較好~
只弄一個完整的base
然後 用namespace的2個function 來分~

"nsControl"#Region "nsControl"
Namespace nsControl
Public Module mControl 
Public Sub FormAutoClose() Sub FormAutoClose(ByVal pform As Form, Optional ByVal piTimeOutInSecond As Integer = 60, Optional ByVal pFunc As DFunc0 = Nothing)
Try
Dim ClsAutoCloseFrom As New ClassAutoCloseForm
If Not pFunc Is Nothing Then
ClsAutoCloseFrom.SetFunc(pFunc)
End If
ClsAutoCloseFrom.SetFrom(pform)
ClsAutoCloseFrom.SetTimerInterval(piTimeOutInSecond)
ClsAutoCloseFrom.StartTimer()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
''' <summary>
''' Do for all control in form
''' </summary>
''' <param name="pControls"></param>
''' <param name="pDFunc1"></param>
''' <remarks></remarks> 
Public Sub FormControlSearch() Sub FormControlSearch(ByVal pControls As System.Windows.Forms.Control.ControlCollection, ByVal pDFunc1 As DFunc1)
Try
For Each lcControl As Control In pControls
If lcControl.Controls.Count > 0 Then
Call pDFunc1(lcControl)
Call FormControlSearch(lcControl.Controls, pDFunc1)
Else
Call pDFunc1(lcControl)
End If
Next
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
''' <summary>
''' when pDFunc1 = true then exit.
''' </summary>
''' <param name="pControls"></param>
''' <param name="pDFunc1"></param>
''' <returns></returns>
''' <remarks></remarks> 
Public Function bFormControlSearch() Function bFormControlSearch(ByVal pControls As System.Windows.Forms.Control.ControlCollection, ByVal pDFunc1 As DFunc1) As Boolean
bFormControlSearch = False
Try
For Each lcControl As Control In pControls
If lcControl.Controls.Count > 0 Then
bFormControlSearch = pDFunc1(lcControl)
If bFormControlSearch Then Exit Function
bFormControlSearch = bFormControlSearch(lcControl.Controls, pDFunc1)
If bFormControlSearch Then Exit Function
Else
bFormControlSearch = pDFunc1(lcControl)
If bFormControlSearch Then Exit Function
End If
Next
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Function
End Module
End Namespace
#End Region

"ClassAutoCloseForm"#Region "ClassAutoCloseForm" ''' <summary>
''' When there is no action in the form in XX seconds. Then AutoClose.
''' Desc :
''' History :
''' 2009-09-02 - Developing
''' </summary>
Public Class ClassAutoCloseForm
Private mTimer As New System.Windows.Forms.Timer
Private miTimerInterval As Integer
Private mForm As Form
Protected mFunc As DFunc0
Private mbIsEnd As Boolean = False

Public Sub StartTimer() Sub StartTimer()
Try
mTimer.Start()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub SetFunc() Sub SetFunc(ByVal pFunc As DFunc0)
Try
mFunc = pFunc
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub SetTimerInterval() Sub SetTimerInterval(ByVal piSecond As Integer)
Try
miTimerInterval = piSecond * 1000
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub SetFrom() Sub SetFrom(ByVal pform As Form)
Try
mForm = pform
mTimer.Interval = 30000
AddHandler mTimer.Tick, AddressOf TimerTick
AddHandler pform.KeyDown, AddressOf FormKeyDown
AddHandler pform.Activated, AddressOf FormActivated
AddHandler pform.Deactivate, AddressOf FormDeactivated
AddHandler pform.MouseMove, AddressOf ControlMouseMove
nsControl.FormControlSearch(mForm.Controls, AddressOf bAddMouseMoveHandler)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Function bAddMouseMoveHandler() Function bAddMouseMoveHandler(ByVal pControl As Control) As Boolean
bAddMouseMoveHandler = False
Try
AddHandler pControl.MouseMove, AddressOf ControlMouseMove
bAddMouseMoveHandler = True
Catch Err As Exception
Debug.Print(Me.GetType.ToString & " - " & Err.ToString)
End Try
End Function

" Handler "#Region " Handler " 
Protected Sub TimerTick() Sub TimerTick(ByVal sender As Object, ByVal e As System.EventArgs)
Try
If mFunc Is Nothing Then
mForm.Close()
Else
mFunc.Invoke()
RemoveHandler mForm.KeyDown, AddressOf FormKeyDown
RemoveHandler mForm.Activated, AddressOf FormActivated
RemoveHandler mForm.Deactivate, AddressOf FormDeactivated
RemoveHandler mForm.MouseMove, AddressOf ControlMouseMove
End If
mTimer.Interval = miTimerInterval
mTimer.Stop()
mTimer.Enabled = False
mbIsEnd = True
RemoveHandler mTimer.Tick, AddressOf TimerTick
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub FormActivated() Sub FormActivated(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Call SetTimer()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub SetTimer() Sub SetTimer()
Try
If mbIsEnd Then
mTimer.Enabled = False
Exit Sub
End If
Call nsTimer.ResetTimer(mTimer, miTimerInterval)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub FormDeactivated() Sub FormDeactivated(ByVal sender As Object, ByVal e As System.EventArgs)
Try
mTimer.Enabled = False
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub FormKeyDown() Sub FormKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Try
Call SetTimer()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Private Sub ControlMouseMove() Sub ControlMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Try
Call SetTimer()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
#End Region
End Class
#End Region
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life