c# winfrom程序检测长时间未操作,返回到登录界面

public MainView()
{

        MyMessager msg = new MyMessager();
        Application.AddMessageFilter(msg);
    }
     static int iOperCount = 0;//记录上时间未操作的时间
    internal class MyMessager : IMessageFilter
    {
        public bool PreFilterMessage(ref Message m)
        {
            //这个函数可以做很多事情,只要是windows消息都会经过,例如注册全局快捷键,修改窗体大小边框,等等
            //也可以调API做对应的事情
            if (m.Msg == WM_KEYDOWN || m.Msg == 513 || m.Msg == 515 ||m.Msg == 516 ||m.Msg == 522
                //|| m.Msg == (int)WindowsMessages.WM_MOUSEMOVE
                //|| m.Msg == (int)WindowsMessages.WM_LBUTTONDOWN
                //|| m.Msg == (int)WindowsMessages.WM_RBUTTONDOWN
                ||m.Msg == WM_KEYUP
                ||m.Msg == WM_RBUTTONDOWN
                )
            {
                iOperCount = 0;
            }
            return false;
        }
    }
       private void timer1_Tick(object sender, EventArgs e)
    {
        iOperCount++;
        this.label2.Text = iOperCount.ToString();//屏幕长时间未操作,累计时间
        int t = fappvars.ScreenLockTime;//获取配置文件中的锁屏时间
        if (iOperCount > t)
        {
            iOperCount = 0;
            this.Hide();
            Login login = new Login();//登录
            login.ShowDialog();//弹出
        }
    }

你可能感兴趣的:(c# winfrom程序检测长时间未操作,返回到登录界面)