⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idletimer.pas

📁 销售帐目管理
💻 PAS
字号:
unit IdleTimer;

interface

uses
  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;

implementation


var
  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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -