想实现Windows程序在一定时间没有键盘鼠标事件发生就自动退出的功能。找过一些以前的贴子,但没看太明白。哪位大侠指点一下。给出点C#的原码来。
解决方案 »
- 微软自带的reportviewer控件 画图表 如折线图问题
- 使用Array.BinarySearch<TKey> 方法遇到问题(TKey为DateTime时)
- 高人来看看:问一个关于串口接收数据的问题
- 关于c#的几个问题?哪位大虾看看啊。。。
- 关于在数据实体类和DataTable间使用反射进行数据转换
- 如何对xml文件作查询?
- 如何在前台显示图片?已经将数据类型为IMAGE的字段IMG取出,三层结构MODEL中的对应着定义的为IMAGE ,前台如何显示出图片?在线等候
- 在C#中用什么命令使数据库记录MoveNext(18)的功能?
- 微软刚刚向法院上交了WINXP的源代码。WINXP竟然是用BASIC语言写的!
- 3DES加密,代码得出的结果跟在线加密得出的结果不一致
- .NET页面设置对话框(PageSetupDialog类)页边距的单位问题
- 问一个特简单的问题关于转义字符的,在线等
设定一个静态变量,保存每次键盘,和鼠标事件的时间!(可以看FORM事件列表)
放一个timer 每10秒(或自定)检查静态变量,时间大于你设定的时间就关闭程序。
这样的话,如果我的程序主窗体中要打开其他窗体,那我就要在所有要打开的窗体中增加程序,控制主窗体的Timer.这样工作量太大了。
我看以前的帖子都争议用钩子处理,可惜我以前没用过。看不太懂。另外我找到一段不错的例子,可我一直没能测试成功,不知道该如何用。哪位大侠可能讲解一下。
http://www.dotnet247.com/247reference/msgs/16/82105.aspx
public class MyMessageFilter : IMessageFilter {
public bool PreFilterMessage(ref Message m) {
if (m.Msg == WM_MOUSEMOVE || m.Msg == WM_KEYDOWN) //可以枚举键盘鼠标信息,具体自己看有关WM消息帮助
{
//更新最后活定时间;
dtLastTime = DateTime.Now()
}
return false;
}
}
然后再程序启动时Application.AddMessageFilter(new MyMessageFilter())
最后在主窗体添加一个定时器定时检查,到时自动关闭就OK了。
老大,写详细点吧。你的代码我编译不过呀。分不够可以再加的。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace RockyZhang
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form,System.Windows.Forms.IMessageFilter
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
private int count=0;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1"; }
#endregion public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
timer1.Stop();
return false;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Form1 frm=new Form1();
Application.Idle+=new EventHandler(frm.IdleFun);
Application.AddMessageFilter(frm);
Application.Run(frm);
}
private void IdleFun(object o,System.EventArgs e)
{
timer1.Start();
} private void timer1_Tick(object sender, System.EventArgs e)
{
if(++count>5)
{
timer1.Stop();
timer1.Dispose();
Application.Exit();
}
}
}
}
谢谢你的回答。
但是如果当我的主窗体打开一了另一个Form,并在这个Form中长时间停留不动,Timer是否还会正常执行。另外您的代码执行有问题,Timer事件无法触发,而程序一直在触发IdleFun事件。
http://www.cnblogs.com/zhenyulu/articles/34044.aspx
我只尝试了两个窗体,无论form2是show或showdialog,timer都会触发。因为触发和停止timer的两个函数都是在applicatoin中进行的,是针对整个应用程序的。
{
if(m.Msg!=0x0113)
{
timer1.Stop();
count=0;
}
return false;
}
micropentium6(小笨|想学ASP)非常谢谢你,你的方法可以实现我的要求,而且方法非常简单方便,很聪明的方法。接分!!:)