C# winfrom 长时间检查不到操作,自动关闭应用程序

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();
            }
        } 

猜你喜欢

转载自blog.csdn.net/fuweiping/article/details/51087897