求助 请高手提示一下 怎么使窗口自动关闭(100分)

  • 主题发起人 主题发起人 cbvfhpje2007
  • 开始时间 开始时间
C

cbvfhpje2007

Unregistered / Unconfirmed
GUEST, unregistred user!
例如一个登录窗口,如果长时不操作,如2分钟,就自己关闭。请高手提示一下delphi实现的思路!
例如 怎么取得没有在文本框输入操作的时间?
 
放一个TTimer-->ontimer事件;
另外在Application.OnIdle进行编程。。。
 
在edit的onchange事件中记录一个时间LastEditTime,可以用GetTickCount,然后在timer中判断GetTickCount-LastEditTime 的值是否大于2分钟,如果是 则Terminite
 
放一个临时变量用来存储输入文本框的时间,然后在TTimer-->ontimer事件里与当前时间进行判断,如果超过2分钟就自动关闭!
 
一个全局变量TimesCount,放一个鼠标钩子,钩子里面把TimeCount清零,
再加一个定时器(每分钟一次),如果超过2,说明2分钟没动了.就关闭.
百分百准确.
 
系统空闲时间检测
我们知道,当用户超过一定的时间没有输入之后,Windows就会启动屏幕保护功能,那么在程序中如何做到这一点呢?就是说我如何检测到用户多久没有输入呢?大家知道,在Windows中有一个Hook技术,就是钩子,我们利用Hook技术,Hook键盘和鼠标,这样就可以知道用户有没有输入了!因此,我们可以修改Timer控件,继承下来就可以了:
用法:
if IdleTimer1.Snooze>6000 then
ShowMessage('用户已经有6秒钟没有输入了!');
///FileName:IdleTimer.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.
 
后退
顶部