为了安全起见,ERP系統常常需要在闲置(用戶沒有键盘和鼠标的操作)一段时间后,自动退出,以防止他人冒名操作。软件在设计时,代码要分几段进行:取得系统闲置时长,用定时器每过一段时间检查一次闲置时长是否达到系统设置的时长,如果达到,就退出系统。下面是 Delphi的实例原码。
閑置时间Delphi的函数
function Tform1.LastInput:Dword;
var
Linput:TLastInputInfo;
begin
result:=0;
try
Linput.cbSize:=SizeOf(TLastInputInfo);
GetLastInputInfo(Linput);
Result:=((GetTickCount - Linput.dwTime) div 1000 );
except
end;
end;
界面判断与退出完整代码。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, AppEvnts, StdCtrls, Spin;
type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
Timer1: TTimer;
Edit1: TEdit;
SpinEdit1: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Mouse : TPoint;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
IdleStart : TDateTime;
implementation
{$R *.dfm}
procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
var Done: Boolean);
begin
IF NOT Timer1.Enabled THEN
BEGIN
Timer1.Enabled := True;
IdleStart := Now;
END;
Done := True;
end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
Var
I : SmallInt;
begin
IF Msg.message = WM_MOUSEWHEEL then
BEGIN
Msg.message := WM_KEYDOWN;
Msg.lParam := 0;
I := HiWord(Msg.wParam);
IF I > 0 THEN
Msg.wParam := VK_UP
ELSE
Msg.wParam := VK_DOWN;
Handled := False;
END
ELSE IF (Msg.Message = WM_MOUSEMOVE) THEN
BEGIN
IF (Timer1.Enabled) AND (Self.Active) THEN
BEGIN
IF (Abs(Msg.pt.X-Mouse.X) > 2) AND (Abs(Msg.pt.Y - Mouse.Y) > 2) THEN
BEGIN
Mouse.X := Msg.pt.X;
Mouse.Y := Msg.pt.Y;
Timer1.Enabled := False;
Edit1.Text := '0';
END;
END;
END
ELSE IF ((Msg.Message = WM_LBUTTONDOWN) OR (Msg.Message = WM_MBUTTONDOWN) OR (Msg.Message = WM_RBUTTONDOWN) OR (Msg.Message = WM_KEYDOWN)) THEN
BEGIN
Timer1.Enabled := False;
Edit1.Text := '0';
END;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
IF Round((Now - IdleStart) * 86400) >= SpinEdit1.Value THEN
Self.Close
ELSE
Edit1.Text := FloatToStr(Round((Now - IdleStart) * 86400));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IdleStart := Now;
end;
end.