定义系统消息类:
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清零