我想做一个类似屏保的程序,如何判断鼠标的静止时间,比如鼠标不动,timer开始计时,到时间就出现一个全黑的form(我把它当作屏保了,呵呵),点击就退出.我遇到的困难是,如何判断鼠标静止不动,如果动了的话就重新计时?

解决方案 »

  1.   

    我按照帖子3楼的做法,可以出现屏保,但是每次都只是在初始化的时候出现一次,不会重复出现. public partial class Form1 : Form
        {
            private static Boolean lockState = false;//保存当前的锁定状态
            private static System.Windows.Forms.Timer appTimer = new Timer(); //计时器
            private static int iTimeLen = 0; //时间计数
            public Form1()
            {
                InitializeComponent();
                RegisterAppLockProcess();
            }
            /// <summary>
            /// 注册应用程序的锁定处理
            /// </summary>
            private static void RegisterAppLockProcess()
            {
                appTimer.Interval = 1000; //每间隔一秒执行一次检查
                appTimer.Tick += new EventHandler(CheckLockState);            appTimer.Start();            LockMessager lm = new LockMessager();            Application.AddMessageFilter(lm);
            }        /// <summary>
            /// 循环检查锁定状态
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private static void CheckLockState(object sender, EventArgs e)
            {            if (lockState) return;            iTimeLen = iTimeLen + 1;            //当五秒内没有接收到鼠标和键盘的按键,则进行锁定
                if (iTimeLen >= 5)
                {
                    //LockProcess();                ShowLockWindow();                lockState = true;
                }
            }        /// <summary>
            /// 显示锁定后的界面
            /// </summary>
            private static void ShowLockWindow()
            {
                //这里简单显示出锁定后的界面
                Form2 frmLock = new Form2();
                frmLock.Show();
            }
    internal class LockMessager : IMessageFilter
            {
                public bool PreFilterMessage(ref Message m)
                {
                    //如果检测到有鼠标或则键盘被按下的消息,则使计数为0.....
                    if (m.Msg == 0x0201 || m.Msg == 0x0100 || m.Msg == 0x0204 || m.Msg == 0x0207 || m.Msg == 0x0216)
                        iTimeLen = 0;                return false;
                }
            }
      

  2.   


    internal class LockMessager : IMessageFilter      
       {           
       public bool PreFilterMessage(ref Message m)     
            {                 //如果检测到有鼠标或则键盘被按下的消息,则使计数为0.....                
     if (m.Msg == 0x0201 || m.Msg == 0x0100 || m.Msg == 0x0204 || m.Msg == 0x0207 || m.Msg == 0x0216)  
    {           
    lockState = false;     
       iTimeLen = 0;   
    }              
      return false;          
       }        
     }
    在你点击鼠标的时候把lockState状态重置成false,不然每次到time时间里面总是return
      

  3.   

    return是回到RegisterAppLockProcess()这个事件吗?为什么要lockstate=false?
    能详细点吗?
      

  4.   

    这边的代码应该有上下文的,lockstate=false表示不是锁定状态