procedure TRTPMSCfgMainfrm.定时器1(Sender: TObject);// 定时器可设定十分之一秒触发一次 var vLastInputInfo: TLastInputInfo; begin vLastInputInfo.cbSize := SizeOf(vLastInputInfo); GetLastInputInfo(vLastInputInfo); if GetTickCount - vLastInputInfo.dwTime > 60000 then //超过一分钟 begin //画圆 end; end;
汗%&*##
SetWindowsHookEx(WH_MOUSE, @HookProc, 0, GetCurrentThreadId);
//DLL
uses
SysUtils,
Windows, {钩子函数都来自 Windows 单元}
Messages, {消息 WM_LBUTTONDOWN 定义在 Messages 单元}
Classes;
{$R *.res}var
gHook: HHOOK; {钩子变量}
gHandle: THandle;const
WM_MouseMsg = WM_User + 1;
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
if gHandle > 0 then
PostMessage(gHandle,WM_MouseMsg,wParam,lParam);
Result := CallNextHookEx(gHook, nCode, wParam, lParam);
end;function SetHook: Boolean; stdcall;
begin
gHook := SetWindowsHookEx(WH_MOUSE, @HookProc, HInstance, 0);
Result := gHook <> 0;
end;function DelHook: Boolean; stdcall;
begin
Result := UnhookWindowsHookEx(gHook);
end;procedure RegisterHandle(AHandle: THandle);stdcall;
begin
gHandle := AHandle;
end;exports
SetHook,
DelHook,
RegisterHandle;//应用unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;const
WM_MouseMsg = WM_USER + 1;type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
private
FIncrement: Int64; //秒
procedure WMMouseMsg(var Msg: TMessage);message WM_MouseMsg;
public
{ Public declarations }
end;var
Form1: TForm1; function SetHook: Boolean; stdcall;external 'MouseHook.dll';
function DelHook: Boolean; stdcall;external 'MouseHook.dll';
procedure RegisterHandle(AHandle: THandle); stdcall;external 'MouseHook.dll'; implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
//
FIncrement := 0;
SetHook;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
//
DelHook;
end;procedure TForm1.FormShow(Sender: TObject);
begin
RegisterHandle(Self.Handle);
end;procedure TForm1.WMMouseMsg(var Msg: TMessage);
begin
FIncrement := 0;
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
Inc(FIncrement);
if FIncrement > 60 then
ShowMessage('超过1分钟未动鼠标!');
end;
hook没必要,毕竟是在自己的应用程序上画的。
mousemove里重新定时
var
vLastInputInfo: TLastInputInfo;
begin
vLastInputInfo.cbSize := SizeOf(vLastInputInfo);
GetLastInputInfo(vLastInputInfo);
if GetTickCount - vLastInputInfo.dwTime > 60000 then //超过一分钟
begin
//画圆
end;
end;