我怎样才能触发Windows中的自动注销窗体应用程序? [英] How can I trigger an auto-logout within a Windows Forms Application?

查看:113
本文介绍了我怎样才能触发Windows中的自动注销窗体应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序项目用户可以向他们的用户名和密码登录。我想使它这样,当用户登录时,我会得到的登录时间,如果用户不使用应用程序30分钟,应用程序将再次发送用户登录画面。我怎样才能做到这一点?

I have a Windows App Project to which users can login with their userid and passwords. I want to make it so that when a user logs in, I will get the Login Time, and if the user doesn't use the application for 30 min, the application will send the user to the Login screen again. How can I achieve this?

推荐答案

这是解决这个问题的简单方法。它的工作以及

This is the simple way to solve this problem. It's working well.

using System;
using System.Windows.Forms;
namespace WindowsApplication1 {
    public partial class Form1 : Form, IMessageFilter {
        private Timer mTimer;
        private int mDialogCount;
        public Form1() {
            InitializeComponent();
            mTimer = new Timer();
            mTimer.Interval = 2000;
            mTimer.Tick += LogoutUser;
            mTimer.Enabled = true;
            Application.AddMessageFilter(this);
        }

        public bool PreFilterMessage(ref Message m) {
            // Monitor message for keyboard and mouse messages
            bool active = m.Msg == 0x100 || m.Msg == 0x101;  // WM_KEYDOWN/UP
            active = active || m.Msg == 0xA0 || m.Msg == 0x200;  // WM_(NC)MOUSEMOVE
            active = active || m.Msg == 0x10;  // WM_CLOSE, in case dialog closes
            if (active) {
                if (!mTimer.Enabled) label1.Text = "Wakeup";
                mTimer.Enabled = false;
                mTimer.Start();
            }
            return false;
        }

        private void LogoutUser(object sender, EventArgs e) {
            // No activity, logout user
            if (mDialogCount > 0) return;
            mTimer.Enabled = false;
            label1.Text = "Time'z up";
        }

        private void button1_Click(object sender, EventArgs e) {
            mDialogCount += 1;
            Form frm = new Form2();
            frm.ShowDialog();
            mDialogCount -= 1;
            mTimer.Start();
        }
    }
}

这篇关于我怎样才能触发Windows中的自动注销窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆