c# - 如果我的 C# 程序中没有任何按键,如何进入登录表单?

标签 c# winforms

我的 C# WinForm 程序中有登录表单和主表单。

当我在主窗体中并且用户在 5 分钟内没有按任何键或移动鼠标时 - 我想转到登录窗体。

如何在 C# WinForm 中实现?

提前致谢

最佳答案

我制作了一个小示例来向您展示如何在完整的应用程序级别实现用户事件检测。诀窍在于使用应用程序消息过滤器。

此示例将在用户 5 分钟不活动时引发消息。

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

namespace WindowsFormsApplication2
{
    static class Program
    {

        private static Timer _idleTimer;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            _idleTimer = new Timer();
            _idleTimer.Tick += new EventHandler(_idleTimer_Tick);
            _idleTimer.Interval = (5 * 60) * 1000; // (5 minutes * seconds) * milliseconds

            Application.AddMessageFilter(new MouseMessageFilter(UserIsActive));
            Application.AddMessageFilter(new KeyboardMessageFilter(UserIsActive));

            Application.Run(new Form1());
        }

        static void _idleTimer_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("You are idle for " + _idleTimer.Interval.ToString() + " milliseconds");
        }

        static void UserIsActive(object sender, EventArgs e)
        {
            _idleTimer.Stop();
            _idleTimer.Start();
        }

        public class MouseMessageFilter : IMessageFilter
        {
            private EventHandler _callback;

            public MouseMessageFilter(EventHandler callback)
            {
                _callback = callback;
            }

            private const int WM_MOUSEMOVE = 0x0200;

            public bool PreFilterMessage(ref Message m)
            {
                if (m.Msg == WM_MOUSEMOVE)
                {
                    _callback(null, null);
                }

                return false;
            }
        }

        private class KeyboardMessageFilter : IMessageFilter
        {
            private EventHandler _callback;

            public KeyboardMessageFilter(EventHandler callback)
            {
                _callback = callback;
            }

            const int WM_KEYDOWN = 0x100;
            const int WM_KEYUP = 0x0101;
            const int WM_SYSKEYDOWN = 0x104;
            const int WM_SYSKEYUP = 0x0105;

            #region IMessageFilter Members

            public bool PreFilterMessage(ref Message m)
            {
                if ((m.Msg == WM_KEYDOWN) || (m.Msg == WM_SYSKEYDOWN))
                {
                    _callback(null, null);
                }

                if ((m.Msg == WM_KEYUP) || (m.Msg == WM_SYSKEYUP))
                {
                    _callback(null, null);
                }

                return false;
            }

            #endregion
        }

    }
}

关于c# - 如果我的 C# 程序中没有任何按键,如何进入登录表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5477939/

相关文章:

c# - 生成由操作过滤器控制的 PDF?

c# - 什么是 "obfuscate"数值的简单但有些有效的方法?

winforms - DataGridViewImageColumn 红色 x

c# - 禁用部分 SaveFileDialog

c# - 需要用户控件中的接受按钮之类的东西

c# - 无法验证 XML 文件的签名,包括 c# 中的注释

c# - Windows 窗体控件 - 字体不会因 Wingding 而改变

c# - 如何在没有 Visual Studio 的系统上编译 .NET 3.5 C# 代码?

c# - 我无法通过双击转到 "EventHandler"

c# - C# WinForms 有 slider 控件吗?