我用VB編寫了一個程序,如何讓用戶在15分鐘沒有使用該程序便自動退出,也就是說15分鐘鼠標或鍵盤沒有動作就關閉程序窗口,代碼該如何寫,放在哪裡

解决方案 »

  1.   

    很多方法
    常见的用TIMER检测吧
      

  2.   

    '取得系統閑置時間
     
    Private Type PointApi
      X As Long
      Y As Long
    End TypePrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointApi) As Long
    Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As LongPrivate Up_Time, Old_Point As PointApiPublic Function FreeTime() As Long
      If GetForegroundWindow = Application.hWndAccessApp Then
         '判斷Mouse是否在使用
         Dim New_Point As PointApi
         GetCursorPos New_Point
         If Old_Point.X <> New_Point.X Or Old_Point.Y <> New_Point.Y Then
            Up_Time = Time
            Old_Point = New_Point
            Exit Function
         End If
         '判斷鍵盤是否在使用
         Dim KeyStat(0 To 255) As Byte
         Call GetKeyboardState(KeyStat(0))
         For I = 0 To 255
             If (KeyStat(I) And &H80) = &H80 Then
                Up_Time = Time
                Exit Function
             End If
         Next
      End If
    Ext:
      FreeTime = DateDiff("s", Up_Time, Time)
    End FunctionPrivate Sub UserControl_Initialize()
      Up_Time = Time
    End Sub
      

  3.   

    楼上正解
    但需要添加TIMER否则程序只能
    执行一次!
      

  4.   

    使用Timer不断检测 设置Interval