Senin, 02 Maret 2015

CARA MEMBUAT AUTOMATIC CLOSE FORM TANPA TIMER

Hi programmer
Terkadang form-form yang kita buat butuh close automatic seperti pada menu admin..
jadi tinggal set waktu nya berapa detik /  menit dan jika di diamkan tanpa ada pergerakan mouse form tersebut akan close dengan sendirinya, jika cursor digerakkan kembali si form tidak akan close dengan sendirinya

berikut codingnya :




CODING DI FORM DESIGNER

 Private _idle As New clsIdle(AddressOf WhenIdle, AddressOf WhenActive, True)
 Private Sub FrmAdmin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  _idle.Start(CInt(Me.TxtTimeOutIdle.Text))
 End Sub

 Sub WhenIdle()
  Me.Dispose()
 End Sub

 Sub WhenActive()
  _idle.Start(CInt(Me.TxtTimeOutIdle.Text))
 End Sub



CODING DI CLASS IDLE

Imports System.Runtime.InteropServices

Public Class clsIdle

 Private Declare Function GetLastInputInfo Lib "User32.dll" (ByRef lastInput As LASTINPUTINFO) As Boolean

 <StructLayout(LayoutKind.Sequential)> _
 Public Structure LASTINPUTINFO
  Public cbSize As Int32
  Public dwTime As Int32
 End Structure

 Public Delegate Sub IdleDelegate()
 Public Delegate Sub ActiveDelegate()
 Private _idlePointer As IdleDelegate
 Private _activePointer As ActiveDelegate

 Private _timeout As Integer
 Private _isDebug As Boolean

 Private WithEvents _timer As New Timer

 Public Sub New(ByVal idleWorker As IdleDelegate, ByVal activeWorker As ActiveDelegate, Optional ByVal isDebug As Boolean = False)
  _idlePointer = idleWorker
  _activePointer = activeWorker
  _isDebug = isDebug
  _timer.Interval = 100
 End Sub

 Public Sub Start(ByVal timeout As Integer)
  _timeout = timeout
  _timer.Start()
 End Sub

 Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _timer.Tick
  Dim lastInput As New LASTINPUTINFO
  lastInput.cbSize = Marshal.SizeOf(lastInput)

  If GetLastInputInfo(lastInput) Then
   If (_timeout <= (Environment.TickCount - lastInput.dwTime) / 1000) Then
    If _isDebug Then Debug.Print("timeout since: " & CDbl(((Environment.TickCount - lastInput.dwTime) / 1000.0) - _timeout).ToString)
    _timer.Stop()
    Call _idlePointer()
   Else
    If _isDebug Then Debug.Print("idle since: " & CDbl((Environment.TickCount - lastInput.dwTime) / 1000.0).ToString)
    Call _activePointer()
   End If
  End If
 End Sub

End Class

Tidak ada komentar:

Posting Komentar