下面的代码是判断多长时间没有用键盘或鼠标的。但是一放到FORM上就会死机。哪位高手可以解决这个问题?(100分)

  • 主题发起人 主题发起人 redted
  • 开始时间 开始时间
R

redted

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码是判断多长时间没有用键盘或鼠标的。但是一放到FORM上就会死机。哪位高手可以解决这个问题?
//以TTimer为基类创建了一个新类,通过它的Snooze来得知用户多长时间没有进行输入操作了
unit ETimer;

interface

uses
Messages, SysUtils, Classes, Controls, StdCtrls, Graphics, Forms,
Dialogs, Buttons, ExtCtrls;

type
TETimer = class(TTimer)
private
{ Private declarations }
function GetSnooze:LongInt;
procedure SetSnooze(const Value:LongInt);
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

uses Windows, WinProcs, WinTypes, ShellApi;
var
Instances:Integer;
ElapsedTime:LongInt;
whKeyBoard,whMouse:HHook;

procedure Register;
begin
RegisterComponents('Sample', [TETimer]);
end;

function MouseHookCallBack(Code:Integer;Msg:Word;MouseHook:LongInt):LongInt;export;
begin
if Code>=0 Then
ElapsedTime:=GetTickCount;
Result:=CallNextHookEx(whMouse,Code,Msg,MouseHook);
end;

function KeyboardHookCallBack(Code:Integer;Msg:Word;KeyboardHook:LongInt):LongInt;export;
begin
if Code>=0 then
ElapsedTime:=GetTickCount;
Result:=CallNextHookEx(whKeyboard,Code,Msg,KeyboardHook);
end;

constructor TETimer.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,GetCurrentTask);
whKeyboard:=SetWindowsHookEx(WH_KEYBOARD,KeyboarHookCallback,
GetMoudleHandleFromInstance,GetCurrentTask);}
whMouse:=SetWindowsHookEx(WH_MOUSE,@MouseHookCallback,
GetModuleHandleFromInstance,GetCurrentThreadID);
whKeyboard:=SetWindowsHookEx(WH_KEYBOARD,@KeyboardHookCallBack,
GetModuleHandleFromInstance,GetCurrentThreadID);
end;
end;

destructor TETimer.Destroy;
begin
Dec(Instances);
if Instances = 0 then
begin
unHookWindowsHookEx(whKeyboard);
unHookWindowsHookEx(whMouse);
end;
inherited Destroy;
end;

procedure TETimer.SetSnooze(Const Value:LongInt);
begin
ElapsedTime:=GetTickCount + Value;
end;

function TETimer.GetSnooze:LongInt;
begin
Result:=GetTickCount - ElapsedTime;
end;

end.

 
我也想知道
 
GetModuleFileName:This function is not available on Windows.
 
后退
顶部