码迷,mamicode.com
首页 > Windows程序 > 详细

C#winform判断鼠标30秒不动就关闭窗口

时间:2017-08-19 11:00:42      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:err   tar   control   key   sep   oid   env   sage   winform   

    public partial class BaseForm : Form
    {
        private Timer timer;
        int x, y;
        DateTime start;
        bool ff = true; 

        public BaseForm()
        {
            timer = new Timer();

            x = Control.MousePosition.X;
            y = Control.MousePosition.Y;

            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        protected void timer_Tick(object sender, EventArgs e)
        {
            int x1 = Control.MousePosition.X;
            int y1 = Control.MousePosition.Y;

            if ((x == x1) && (y == y1) && ff)
            {
                start = DateTime.Now;
                ff = false;
            }
            if (x != x1 || y != y1)
            {
                x = x1;
                y = y1;
                start = DateTime.Now;
                ff = true;
            }
            TimeSpan ts = DateTime.Now.Subtract(start);
            if (ts.Seconds > 5) Environment.Exit(0);  //把5改成30,就是30秒
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            start = DateTime.Now;
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }

 

C#winform判断鼠标30秒不动就关闭窗口

标签:err   tar   control   key   sep   oid   env   sage   winform   

原文地址:http://www.cnblogs.com/lee24789229/p/7395491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!