比如一段时间不动就自动退出一样!解决了就给分,决不失言!

解决方案 »

  1.   

    还有什么别的方法吗》?
    今天有客户抱怨我们的程序不安全,说人走了以后不能自动退出。没有办法,谁能帮我想想设置一个timeout,如果用户一定时间没有动键盘鼠标,就退出程序。
      

  2.   

    楼主可以在Application.Idle事件里作考虑.
      

  3.   

    Hook Mouse and Keyboard
      

  4.   

    结合这2个帖子
    http://www.cnblogs.com/michaelxu/archive/2006/09/22/511557.aspx
    http://support.microsoft.com/default.aspx?scid=kb;en-us;318804
      

  5.   

    http://dev.csdn.net/article/49/49696.shtm
      

  6.   

    定义一个Timer,当TimeOut时退出。当有键盘或鼠标动作时调用Timer.start()方法清零。
      

  7.   

    private int tick=0;private override void WndProc(ref Message msg)
    {
       base.WndProc(ref msg);
       tick=Environment.TickCount;
    }
    Timer事件里写判断代码,记的给我分啊!!
    if(Environment.TickCount-tick>5000) this.Close();
      

  8.   

    楼上的写法有点问题先判断键盘鼠标是否动了
    然后在Application.Idle事件里作考虑.
      

  9.   

    可以设置session值,比如登陆时保存登陆的用户ID,在一些频繁按钮或操作上加上session值是否为null的判断,为null就退出。我现在的项目就这样的,一般session值的超时为20分钟
      

  10.   

    可以订阅MouseEventHandler(MouseMove)时间,来看是鼠标是否移动了
      

  11.   

    如果你建立的是一个application,那么可以放一个Timer控件,然后在OnTimer的事件中定时的接受键盘传来的消息(SendKey),具体我现在也说不清,好久没做UI了。
      

  12.   

    参见:
    http://www.codeproject.com/csharp/ApplicationIdle.asp
      

  13.   

    同意tcxx2008(小小方) :
    ===========================
    可以设置session值,比如登陆时保存登陆的用户ID,在一些频繁按钮或操作上加上session值是否为null的判断,为null就退出。我现在的项目就这样的,一般session值的超时为20分钟
    =============================
      

  14.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace Mouse_Move
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private int tick=0;        protected override void WndProc(ref Message msg)
            {
               base.WndProc(ref msg);
               tick=Environment.TickCount;
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if(Environment.TickCount-tick>2000) this.Close();            
            }
        }
    }
      

  15.   

    http://www.codeproject.com/managedcpp/rtwidledll.asp
      

  16.   

    别忘了把timer 的Enabled 属性设置成true;
      

  17.   

    这个方法应该可以:(设若程序5分钟没有键盘和鼠标动作就退出)
    先设置一个变量来存放多久没有键盘和鼠标输入就退出程序,如 int timeExit=5
    再设置一个变量来存放当前已有多久没有键盘和鼠标输入,如 int timeNow=0(初始值为0)
    然后拖一个timer控件,将其Interval属性设为1000。双击Timer控件,在timer1_Tick对应的事件中输入:
    timeNow=timeNow+1;
    if(timeNow>=timeExit) this.close();
    然后就是当有键盘或鼠标动作时就将tiemNow清零,使其重新计数。因为有鼠标动作时,鼠标肯定会移动,因此在程序窗口(默认为Form1)的Form1_MouseMove事件中输入:
    timeNow=0;
    this.timer1.Enable=false;
    this.timer1.Enable=true;
    之所以要将控件timer1先禁用再启用,是为让其重新开始计算时间。
    同样,有键盘动作时都会有键按下,因此将程序窗口(默认为Form1)的Form1_KeyPress事件设置为Form1_MouseMove即可。
    注意:这里所说的没有键盘或鼠标动作是指在程序具有焦点时的情况,若程序不具有焦点,就算有键盘或鼠标动作也会退出,不过这个可以通过在其退出之前使其具有焦点,并寻问用户是否退出。
      

  18.   

    不好意思,前面有一些错误:Interval为1000时时间为1秒钟,还有一些输入上错误,下面是代码是经过测试的,5秒没键盘和鼠标输入就提示是否退出,且若3秒不回答就会自动退出。
    总共用了两个timer控件,第二个控制提示对话框。第一个timer1的启动时的Enable属性为true,而第二个的为false。
            private int timeExit = 5;
            private int timeExit2 = 3;
            private int timeNow = 0;
            private int timeNow2 = 0;
            private void timer1_Tick(object sender, EventArgs e)
            {
                timeNow = timeNow + 1;
                if (timeNow >= timeExit)
                {
                    this.timer1.Enabled = false;
                    this.timer2.Enabled = true;
                    DialogResult exitResult = MessageBox.Show("您真的要退出吗?", "确认", MessageBoxButtons.YesNo);
                    if (exitResult == DialogResult.Yes)
                        this.Close();
                    else
                    {
                        this.timeNow = 0;
                        timer1.Enabled = true;
                        this.timer2.Enabled = false;
                        this.timeNow2 = 0;
                    }
                }
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                timeNow = 0;
                this.timer1.Enabled = false;
                this.timer1.Enabled = true;
            }        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                timeNow = 0;
                this.timer1.Enabled = false;
                this.timer1.Enabled = true;
            }        private void timer2_Tick(object sender, EventArgs e)
            {
                this.timeNow2 = this.timeNow2 + 1;
                if (timeNow2 >= timeExit2)
                    this.Close();
            }
    这是我在CSDN上第一次回答问题,希望能被采纳!
      

  19.   

    我是一个新手,刚学没几天,我来说说我的想法
    设立一个Timer,和变量n
    然后在form的MouseMov和KeyPress事件被触发则修改n为0。
    Timer设置成每隔一段时间则变量n++,然后当n到达一定的数值后则关闭程序。这就是我的思路,不知道是否可行。
      

  20.   

    上面的一些用Timer的回答,我总觉得有些问题,都是用Form的鼠标、键盘事件,那如果一个程序有N个Form,不是每个Form都要加这些Timer和事件处理了吗?若,一个Form打开另一个Form,那这个时间如何算?还有,有时候,会调用一些自动生成的Form,而不是在IDE中建好的,怎么处理?
    尤其是,可能产生同时有多个Timer都在计算,都有各自的变量,Form1打开Form2,Form1的Timer没有停止计算,但是,此时,是Form2有鼠标和键盘事件,那么,Form1不是肯定会在5分钟关闭程序吗?