AFK 2分钟并关闭表格 [英] AFK 2 minutes and close form

查看:85
本文介绍了AFK 2分钟并关闭表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

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分钟并关闭表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆