您当前的位置: 首页 >  c#

ITKEY_

暂无认证

  • 0浏览

    0关注

    732博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C# 无操作则退出登陆

ITKEY_ 发布时间:2022-07-22 17:45:33 ,浏览量:0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace LockScreenMsg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Height = 200;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }

        [DllImport("user32.dll")]
        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        static long GetLastInputTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref vLastInputInfo))
            {
                return 0;
            }
            return Environment.TickCount - (long)vLastInputInfo.dwTime;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        public string isNull = null;
        private void timer1_Tick(object sender, EventArgs e)
        {
           long time=GetLastInputTime()/1000;
            
            this.label1.Text = string.Format("用户已经{0}秒没有操作了", time);
           if (time >= 20)
           {
                this.Close();
           }
        }
    }
}

参考

https://www.cnblogs.com/snail0404/p/6433125.html

关注
打赏
1665243900
查看更多评论
立即登录/注册

微信扫码登录

0.0419s