WinForm 鼠标在页面无操作时页面关闭

public partial class Demo: Form
    {

     /// <summary>
        /// 鼠标停留页面时间计时
        /// </summary>
        System.Windows.Forms.Timer time_stop;
        /// <summary>
        /// 记录鼠标的x坐标和y坐标
        /// </summary>
        int x, y;
        /// <summary>
        /// 计时器开始
        /// </summary>
        DateTime start;

  public Demo()
        {
            InitializeComponent(); 
            time_stop = new Timer();
            time_stop.Interval = 1000;
            time_stop.Start();
            time_stop.Tick += new EventHandler(time_stop_Tick);
            x = Control.MousePosition.X;//扑捉鼠标刚开始时X轴坐标
            y = Control.MousePosition.Y;//扑捉鼠标刚开始时Y轴坐标
        }

        #region  判断鼠标在20分钟内是否移动
        int dTime = 59;
        private void time_stop_Tick(object sender, EventArgs e)
        {
                int x1 = Control.MousePosition.X;//当前鼠标X轴
                int y1 = Control.MousePosition.Y;//当前鼠标Y轴
                if ((x == x1) && (y == y1))//初始鼠标位置与当前位置相等时
                {
                    start = DateTime.Now;
                    dTime--;
                }
                if (x != x1 || y != y1)//初始鼠标位置与当前位置不等时
                {
                    x = x1;
                    y = y1;
                    start = DateTime.Now;
                    dTime = 59;
                }
                if (dTime == 0)//计时器为0时
                {
                    this.Close();
                    dTime = 59;
                }
                else
                {
                    lblTimeDown.Text = dTime.ToString() + "秒后返回";
                }
        }
        #endregion

 }

posted @ 2013-01-16 09:35  liyx0618  阅读(399)  评论(0编辑  收藏  举报