程序处于开启状态的时候,如果长时间(如:20分钟)没有操作则退出程序,怎么检测?
我搜到两种方法可都不知道怎么用:
一种是用键盘钩子Hook
一种是用Application.AddMessageFilter
希望高手指点

解决方案 »

  1.   

    用钩子来实现...只要发现键盘 鼠标就设置时间变量为 系统时间
    在拉个TIMER 来检测时间变量如果和系统时间错20分钟 退出程序
      

  2.   

    用Application.AddMessageFilter就可以了,加入0x0200、0x0201...等消息再用一个定时器循环进行检测,若超过某一个时间仍无操作,则隐藏所有窗体或直接退出
      

  3.   

    用Application.AddMessageFilter监视键盘消息放一个timer,只有Application.AddMessageFilter监视到消息后,timer就停止运行并清零.否则timer开始计时
    在timer1_Tick里判断累计20分钟后Application.Exit就行了
      

  4.   

    生成一个消息类,处理你自己想要的消息即可啊!
    internal class MyMessager : IMessageFilter
            {
                public bool PreFilterMessage(ref Message m)
                {
                    //如果检测到有鼠标或则键盘的消息,则使计数为0.....
                    if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
                    {
                        iOperCount = 0;
                    }                return false;
                }
            }
    消息检测里这样写:MyMessager msg = new LockMessager();
    Application.AddMessageFilter(msg);
    定时器里不停的对iOperCount 进行加1操作,然后判断总时间是否超过你要的时间段,超过则直接退出
    Application.Exit()明白了没?
      

  5.   

    消息检测有点笔误,应该如下:MyMessager msg = new MyMessager();
    Application.AddMessageFilter(msg);
      

  6.   

    Application.AddMessageFilter()貌似只对本程序有用,最小化后鼠标和键盘怎么动也不会触发
      

  7.   

    Application.AddMessageFilter() 是截获本程序向系统发出的消息,和挂钩HOOK是不一样的
    public Form1()
            {
                InitializeComponent();
                MyMessager msg = new MyMessager();
                Application.AddMessageFilter(msg);
                timer1.Start();
            }        static int iOperCount = 0;
            internal class MyMessager : IMessageFilter
            {
                public bool PreFilterMessage(ref Message m)
                {
                    //如果检测到有鼠标或则键盘的消息,则使计数为0.....
                    if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
                    {
                        iOperCount = 0;
                    }                return false;
                }
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                iOperCount++;
                if (iOperCount > 100)
                {
                    Application.Exit();
                }
            }