unit Test_Free_Unit;
interface
uses
Classes, Windows, Messages, SysUtils;
type
TTest_Free = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(hMain: hWnd; iTime: Integer);
// destructor Destroy; override;
end;
var
TimeID: DWORD;
MousePos: TPoint;
Main_Handle: hWnd; //主窗体句柄
hNextHookProc: HHook = 0;
Count_Time, Test_Time: Integer;
const
hStart_Time = 1;
hPause_Time = 2;
hReset_Time = 3;
WM_USER_TimeOut = WM_USER + 100;
implementation
uses Unit1;
{ Test_Free }
function HookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
const
WM_KEY_FIRST = $0100; //第一个键盘消息
WM_KEY_LAST = $0108; //最后一个键盘消息
WM_MOUSE_FIRST = $0200; //第一个鼠标消息
WM_MOUSE_LAST = $020A; //最后一个鼠标消息
var
Pos: TPoint;
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
if (PEventMsg(lparam)^.message >= WM_KEY_FIRST) and (PEventMsg(lparam)^.message <= WM_KEY_LAST) then //键盘消息
Count_Time := 0
else
begin
if (PEventMsg(lparam)^.message >= WM_MOUSE_FIRST) and (PEventMsg(lparam)^.message <= WM_MOUSE_LAST) then //鼠标消息
begin
GetCursorPos(Pos);
if (Abs(MousePos.X - Pos.X) > 10) or (Abs(MousePos.Y - Pos.Y) > 10) then
begin
Count_Time := 0;
GetCursorPos(MousePos);
end;
end;
end;
end;
constructor TTest_Free.Create(hMain: hWnd; iTime: Integer);
begin
Count_Time := 0;
Test_Time := iTime;
Main_Handle := hMain;
GetCursorPos(MousePos);
inherited Create(False);
end;
{
destructor TTest_Free.Destroy;
begin
inherited Destroy;
end;
}
procedure TTest_Free.Execute;
{
声明:procedure test(var Msg: TMessage); message WM_USER_TimeOut;
使用:TTest_Free.Create(Handle, 5000);
}
var
Msg: TMsg;
Events: array[0..2] of THandle;
begin
TimeID := SetTimer(0, 0, 1000, nil); //创建定时器
hNextHookProc := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0); //挂消息钩子
Events[0] := hStart_Time; Events[1] := hPause_Time; Events[2] := hReset_Time;
while not Terminated do
begin
Case MsgWaitForMultipleObjects(3, Events, False, QS_TIMER, INFINITE) of
WAIT_OBJECT_0: //开始
if Suspended then Resume;
WAIT_OBJECT_0 + 1: //暂停
Suspend;
WAIT_OBJECT_0 + 2: //重新开始
if Suspended then Resume;
WAIT_OBJECT_0 + 3: //WM_TIMER消息
begin
Form1.Memo1.Lines.Add(inttostr(Count_Time));
GetMessage(Msg, 0, WM_TIMER, WM_TIMER); //必须有消息循环
Count_Time := Count_Time + 1;
if Count_Time >= Test_Time then
begin
PostMessage(Main_Handle, WM_USER_TimeOut, 0, 0);
Break;
end;
end;
end;
end;
UnHookWindowsHookEx(hNextHookProc);
KillTimer(0, TimeID);
end;
end.
以前乱写的。不知道有没有问题。自己测试吧。有问题说一下~