1\我想做一个在鼠标,键盘20分钟以后没有动后提示的程序!那位能否给我源代码!
2\程序最小化在后面。不影响当前电脑操作。
找了三天资料还没有成功,满发给为给个源代码,谢谢!

解决方案 »

  1.   

    转一个
      监测系统多少时间没有用户输入    
        
    系统空闲时间检测 Idle我们知道,当用户超过一定的时间没有输入之后,Windows就会启动屏幕保护功能,那么在程序中如何做到这一点呢?就是说我如何检测到用户多久没有输入呢?大家知道,在Windows中有一个Hook技术,就是钩子,我们利用Hook技术,Hook键盘和鼠标,这样就可以知道用户有没有输入了!因此,我们可以修改Timer控件,继承下来就可以了:用法:if IdleTimer1.Snooze>6000 thenShowMessage('用户已经有6秒钟没有输入了!');///FileName:IdleTimer.Pasunit IdleTimer;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ExtCtrls;typeTIdleTimer = class(TTimer)privatefunction GetSnooze: Longint;procedure SetSnooze(const Value: Longint);{ Private declarations }protected{ Protected declarations }public{ Public declarations }constructor Create(AOwner:TComponent);override;destructor Destroy;override;property Snooze:Longint read GetSnooze write SetSnooze;published{ Published declarations }end;procedure Register;implementationvarInstances:integer;ElapsedTime:Longint;whKeyBoard,whMouse:HHook;procedure Register;beginRegisterComponents('System', [TIdleTimer]);end;{ TIdleTimer }function MouseHookCallBack(Code:integer;Msg:lParam;MouseHook:wParam):DWORD;stdcall;beginif Code>=0 then ElapsedTime :=GetTickCount;Result := CallNextHookEx(whMouse,Code,Msg,MouseHook);end;function KeyBoardCallBack(Code:integer;Msg:word;KeyBoardHook:Longint):LongInt;stdcall;beginif Code>=0 then ElapsedTime :=GetTickCount;Result := CallNextHookEx(whKeyBoard,Code,Msg,KeyBoardHook);end;constructor TIdleTimer.Create(AOwner: TComponent);function GetModuleHandleFromInstance:THandle;vars:array[0..512] of char;beginGetModuleFileName(HInstance,s,SizeOf(s)-1);Result :=GetModuleHandle(s);end;begininherited Create(AOwner);Inc(Instances);if Instances =1 then beginElapsedTime :=GetTickCount;whMouse := SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,GetModuleHandleFromInstance,0);whKeyBoard :=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardCallBack,GetModuleHandleFromInstance,0);end;end;destructor TIdleTimer.Destroy;beginDec(Instances);if Instances =0 then beginUnhookWindowsHookEx(whKeyBoard);UnhookWindowsHookEx(whMouse);end;inherited;end;function TIdleTimer.GetSnooze: Longint;beginResult:= GetTickCount - ElapsedTime;end;procedure TIdleTimer.SetSnooze(const Value: Longint);beginElapsedTime := GetTickCount + Value;end;end.
     
       
      

  2.   

    // 试试 :unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,IdleTimer, ExtCtrls, StdCtrls ;type
      TForm1 = class(TForm)
        Timer1: TTimer;
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      IdleTimer1 : TIdleTimer ;
      i : Integer ;implementation{$R *.dfm}
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      IdleTimer1 := TIdleTimer.Create(Self) ;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      if IdleTimer1.Snooze > 1200000  then Caption := '20 Mins ' ;
      Else
      begin
        Inc(i) ;
        Caption := 'Living' + '    ' + IntToStr(i) ;
      end ;
    end;end.-------------------
    // From my Friend ;unit IdleTimer;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls;type
      TIdleTimer = class(TTimer)
      private
        function GetSnooze: Longint;
        procedure SetSnooze(const Value: Longint);
        { Private declarations }
      protected
        { Protected declarations }
      public
        { Public declarations }
        constructor Create(AOwner:TComponent);override;
        destructor Destroy;override;
        property Snooze:Longint read GetSnooze write SetSnooze;
      published
        { Published declarations }
      end;procedure Register;implementationvar
      Instances:integer;
      ElapsedTime:Longint;
      whKeyBoard,whMouse:HHook;procedure Register;
    begin
      RegisterComponents('System', [TIdleTimer]);
    end;{ TIdleTimer }function MouseHookCallBack(Code:integer;Msg:lParam;MouseHook:wParam):DWORD;stdcall;
    begin
      if Code>=0 then ElapsedTime :=GetTickCount;
      Result := CallNextHookEx(whMouse,Code,Msg,MouseHook);
    end;function KeyBoardCallBack(Code:integer;Msg:word;KeyBoardHook:Longint):LongInt;stdcall;
    begin
      if Code>=0 then ElapsedTime :=GetTickCount;
      Result := CallNextHookEx(whKeyBoard,Code,Msg,KeyBoardHook);
    end;constructor TIdleTimer.Create(AOwner: TComponent);
      function GetModuleHandleFromInstance:THandle;
      var
        s:array[0..512] of char;
      begin
        GetModuleFileName(HInstance,s,SizeOf(s)-1);
        Result :=GetModuleHandle(s);
      end;begin
      inherited Create(AOwner);
      Inc(Instances);
      if Instances =1 then begin
        ElapsedTime :=GetTickCount;
        whMouse := SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,GetModuleHandleFromInstance,0);
        whKeyBoard :=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardCallBack,GetModuleHandleFromInstance,0);
      end;
    end;destructor TIdleTimer.Destroy;
    begin
      Dec(Instances);
      if Instances =0 then begin
        UnhookWindowsHookEx(whKeyBoard);
        UnhookWindowsHookEx(whMouse);
      end;
      inherited;
    end;function TIdleTimer.GetSnooze: Longint;
    begin
      Result:= GetTickCount - ElapsedTime;
    end;procedure TIdleTimer.SetSnooze(const Value: Longint);
    begin
      ElapsedTime := GetTickCount + Value;
    end;end.
      

  3.   

    我条是通过!但是根本就没有结果出来!
    unit IdleTimer;
    是不是新创建unit,然后拷贝代码即可
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      AppEvnts, ExtCtrls;type
      TForm1 = class(TForm)
        ApplicationEvents1: TApplicationEvents;
        Timer1: TTimer;
        procedure ApplicationEvents1Message(var Msg: tagMSG;
          var Handled: Boolean);
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    var LastUserActionTime: Cardinal = 0;procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    begin
      if ((Msg.Message >= WM_KEYFIRST) and (Msg.Message <= WM_KEYLAST)) or
         ((Msg.Message >= WM_MOUSEFIRST) and (Msg.Message <= WM_MOUSELAST)) then
        LastUserActionTime := GetTickCount;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      LastUserActionTime := GetTickCount;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      if GetTickCount - LastUserActionTime > 5 * 1000 then  // 超过5秒用户不操作即关闭
        Close;
    end;end.
      

  5.   

    还是没法检测键盘,和鼠标,无论是否有键盘鼠标事件都会执行 close
    if GetTickCount - LastUserActionTime > 5 * 1000 then  // 超过5秒用户不操作即关闭
        Close;到底为什么
      

  6.   

    把楼上的改一下再试试if GetTickCount - LastUserActionTime > 1200000 then  // 超20分钟出现提示
       showmessage('用户已经有20分钟没有输入了');         //调试的时候将时间改小点
      

  7.   

    上面的是用于主窗体的,
    用于其他地方的请自行修改,
    必要时还ApplicationEvents1Message中设Handled为false