大家好,
AFK 2分钟后如何自动关闭表单(无鼠标移动,无输入)
How can I auto close form after 2 minutes AFK (no mouse movement, no typing)
帮帮我
推荐答案
您可以使用GetLastInputInfo win32 Api来检测键盘和鼠标上没有用户输入在给定的时间内.下面是一个可以在新表单项目中尝试的示例.
You can use the GetLastInputInfo win32 Api to detect no user input on the keyboard and mouse for a given amount of time. Below is an example that you can try in a new form project. Imports System.Runtime.InteropServices Public Class Form1 Private WithEvents Timer1 As New Timer With {.Interval = 1000, .Enabled = True} Private LII As New LASTINPUTINFO() With {.cbSize = CUInt(Marshal.SizeOf(LII))} Private WaitMinutes As Double = 2.0 'The time in minutes to wait after no user input is detected <DllImport("user32.dll")> Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function <StructLayout(LayoutKind.Sequential)> Private Structure LASTINPUTINFO Public cbSize As UInteger Public dwTime As UInteger End Structure Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick GetLastInputInfo(LII) Me.Text = CInt((Environment.TickCount - LII.dwTime) / 1000).ToString 'this line is just for testing, it can be removed If CInt((Environment.TickCount - LII.dwTime) / 1000) >= (WaitMinutes * 60) Then Timer1.Stop() Timer1.Dispose() Me.Close() End If End Sub End Class
更多推荐
AFK 2分钟并关闭表格
发布评论