zoukankan      html  css  js  c++  java
  • winfrom 倒计时控件

    最近在做一个快递柜项目,要求在用户没有操作的时间到了一分钟,自动返回主页,我于是封装了一个倒计时控件,废话少说,直接上代码

     public partial class RemainingTimeUC : UserControl
        {
    
            public RemainingTimeUC()
            {
                InitializeComponent();
            }
            Form ParantForm = null;
            private void RemainingTimeUC_Load(object sender, EventArgs e)
            {
               //获得用户控件所在的窗体
                ParantForm = this.FindForm();
            }
            #region timerUC_Tick事件
            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);
                }
    
            }
            #endregion
        }

      

    //
    //此方法不知道是哪位前辈写的,借用一下
    //
    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;
    }
    }
    

      

    小于60秒的图实在是截不到。。。。。

  • 相关阅读:
    Tomcat单机多实例部署
    通过Zabbix监控Tomcat单机多实例
    Swarm容器集群管理(超详细)
    15、基于consul+consul-template+registrator+nginx实现自动服务发现
    14、Docker监控方案(Prometheus+cAdvisor+Grafana)
    创建python虚拟环境
    Linux系统基础优化及常用命令
    Linux常用命令--用户管理,文件权限,打包命令等
    Linux--CentOS7使用firewalld打开关闭防火墙与端口
    vim
  • 原文地址:https://www.cnblogs.com/saodiseng2015/p/4577186.html
Copyright © 2011-2022 走看看