zoukankan      html  css  js  c++  java
  • 获取键盘和鼠标没有操作的时间

    public class MouseKeyBoardOperate
    
    {
    
    /// <summary>
    
    /// 创建结构体用于返回捕获时间
    
    /// </summary>
    
    [StructLayout(LayoutKind.Sequential)]
    
    struct Lastinputinfo
    
    {
    
    /// <summary>
    
    /// 设置结构体块容量
    
    /// </summary>
    
    [MarshalAs(UnmanagedType.U4)]
    
    public int cbSize;
    
     
    
    /// <summary>
    
    /// 抓获的时间
    
    /// </summary>
    
    [MarshalAs(UnmanagedType.U4)]
    
    public uint dwTime;
    
    }
    
     
    
    [DllImport("user32.dll")]
    
    private static extern bool GetLastInputInfo(ref Lastinputinfo plii);
    
     
    
    /// <summary>
    
    /// 获取键盘和鼠标没有操作的时间
    
    /// </summary>
    
    /// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
    
    public static long GetLastInputTime()
    
    {
    
    var vLastInputInfo = new Lastinputinfo();
    
    vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
    
    if (!GetLastInputInfo(ref vLastInputInfo))
    
    {
    
    return 0;
    
    }
    
    else
    
    {
    
    var count = Environment.TickCount - (long)vLastInputInfo.dwTime;
    
    var icount = count / 1000;
    
    return icount;
    
    }
    
    }
    

      

     private void timerUC_Tick(object sender, EventArgs e)
    
           {
    
               try
    
               {
    
                   int TotalSecond = 60;//设置时间为60s
    
                   var remainTime = TotalSecond - MouseKeyBoardOperate.GetLastInputTime();//总时间-用户未操作的时间
    
                   this.labRT.Text = remainTime.ToString(CultureInfo.InvariantCulture);//显示离关闭的剩余时间
    
     
    
                   if (remainTime == 30)
    
                   {
    
                       this.labRT.ForeColor = Color.Orange;
    
                   }
    
                   if (remainTime <= 9)
    
                   {
    
                       this.labRT.ForeColor = Color.Red;
    
                       this.labRT.Text = "0" + remainTime;
    
                   }
    
                   if (remainTime == 59)
    
                       this.labRT.ForeColor = Color.White;
    
                   if (remainTime == 0)
    
                   {
    
                       ParantForm.Close();
    
                   }
    
               }
    
               catch (Exception ex)
    
               {
    
                   LogHelper.WriteLog(this.Name + "timerUC_Tick", ex.Message);
    
               }
    
     
    
           }
    

      

  • 相关阅读:
    26 转义符 re模块 方法 random模块 collection模块的Counter方法
    25 正则表达式
    24 from 模块 import 名字
    24 from 模块 import 名字
    24 from 模块 import 名字
    23 析构方法 items系列 hash方法 eq方法
    21 isinstance issubclass 反射 _str_ _new_ _len_ _call_
    20 属性, 类方法, 静态方法. python2与python3的区别.
    python(1)
    python之字符串格式化
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/6899757.html
Copyright © 2011-2022 走看看