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

 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秒的图实在是截不到。。。。。

winfrom 倒计时控件的更多相关文章

  1. android自定义倒计时控件示例

    这篇文章主要介绍了Android秒杀倒计时自定义TextView示例,大家参考使用吧 自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.co ...

  2. Android 自定义倒计时控件CountdownTextView

    效果如下: 剩余 00:59:21 package com.bihu.advertiserapp.widgets; import android.annotation.TargetApi; impor ...

  3. android中倒计时控件CountDownTimer分析

    android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long milli ...

  4. WinFrom 公共控件 Listview 的使用

    Listview绑定数据库数据展示与操作使用  1.拖一个Listview控件到项目中先将视图改为Details 2.编辑列 设置列头  添加columnHeader成员 Text 是显示的名称 3. ...

  5. winFrom 常用控件属性及方法介绍

    目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...

  6. 野比的示波器案例(Winfrom用户控件)

    使用该用户控件做的效果图,如果数据正确,可实现 波形.直线.等等效果图...... 对于本程序的认识还是不够深彻.如果有其他方法或算法,欢迎讨论下.将我所能理解的代码都再次标识了一番. ------- ...

  7. WinFrom ProgressBar控件的使用

    在WinForm程序中,大多数情况下我们是知道程序运行所需要的时间或步骤的,比如批量复制文件时文件的数量,数据导出或导入时数据的总行数等等.对于步骤比较确定的操作,如果程序执行过程时间较长,很容易使用 ...

  8. WinFrom 第三方控件 TeleRik控件

    1.首先从工具-拓展与应用中下载安装  TeleRik WinFroms VsExtensions   TeleRik dll文件     2.工具箱控件  将Telerik控件更新过来 3.新建一个 ...

  9. winfrom浏览器控件

    (1)webbrowser 在ie的基础上开发出来的,一般情况下很好用,特殊情况下没法用,一堆坑,h5支持效果不好 使用:直接拖控件就好了 (2)WebKit .NET http://webkitdo ...

随机推荐

  1. iOS:获取图片Alpha图片

    -(void)createImages { // Load the alpha image, which is just the same Ship.png image used in the cli ...

  2. 制作openstack用的centos6.5镜像

    目的: 在centos6.5操作系统环境下制作一个centos6.5的kvm镜像,安装cloud-init,能自己主动扩展根分区 一.制作环境: 操作环境是在openstack平台开一个实例.装的是c ...

  3. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  4. android解析xml一直报错org.xmlpull.v1.XmlPullParserException

     错误: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT @1:2 injava.io.String ...

  5. Delphi 获取网站验证码的图片

    uses ActiveX,ComObj; procedure TfrmMain.FormCreate(Sender: TObject); begin OleInitialize(nil); end; ...

  6. Could not find or load main class

    Then add '.' to your $CLASSPATH with CLASSPATH=.:$CLASSPATH or as a paramater with java -classpath . ...

  7. mooon编译系统介绍(可复用Makefile)

    mooon编译系统介绍(可复用Makefile).pdf(ChinaUnix下载) CSDN下载:http://download.csdn.net/detail/aquester/5626929 mo ...

  8. vs2008编译QT开源项目--太阳神三国杀源码分析(三) 皮肤

    太阳神三国杀的界面很绚丽,界面上按钮的图标,鼠标移入移出时图标的变化,日志和聊天Widget的边框和半透明等效果,既可以通过代码来控制,也可以使用皮肤文件qss进行控制.下面我们分析一下三国杀的qss ...

  9. 14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚

    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚 如果自动提交模式被启用,在InnoDB里, 所有的用户活动发生在一个事务里, 每个SQL语句 ...

  10. JS Bin Tips and Bits • About

    JS Bin Tips and Bits • About   About Who built this? JS Bin was built by Remy Sharp and is completel ...