winform操作空闲一段时间自动注销到登录界面

定义系统消息类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DependableRecords.Operation
{
    internal class MyMessager : IMessageFilter
    {
      
        //截取鼠标、键盘消息,进行处理
        public bool PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            //513,左键;516,右键;519,滚轮点击;520滚轮点击轮动;256,回车;257,其他键盘键。522,滚轮滚动
            if (m.Msg != 513 && m.Msg != 516 && m.Msg != 519 && m.Msg != 520 && m.Msg != 522 && m.Msg != 256 && m.Msg != 257)
            {
            }
            else
            {
                Operation.OPParameter.TimeNumber = 0;
            }
            return false;
        }
    }
}

定义监控类:

internal class OPFormTime
    {
        private static Timer time = new Timer();

        public static Timer Times
        {
            get { return time; }
        }

        public static void SetTime()
        {
            time.Interval = 1000;
            time.Tick += new EventHandler(time_Tick);
        }

        private static void time_Tick(object sender, EventArgs e)
        {
            Operation.OPParameter.TimeNumber++;
            int num = Operation.OPParameter.TimeNumber;
            if (num == 自定义空闲*60)
            {
                num = 0;
                Application.Restart();
            }
        }
    }

窗体load事件加载窗体消息:

  private void PageLoad()
        {
            Operation.OPFormTime.SetTime();
            Operation.OPFormTime.Times.Start();
            Application.AddMessageFilter(new Operation.MyMessager());
        }

子窗体打开注意将Operation.OPParameter.TimeNumber清零

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值