我的 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/