delphi 锁定计算机,使用Delphi检测用户在Windows 7中锁定/解锁屏幕的时间

您的代码无效,因为您没有正确实现它.

您没有正确声明WTSRegisterSessionNotification()和WTSUnRegisterSessionNotification().

此外,您没有考虑VCL在Form对象的生命周期中动态重新创建窗体窗口的可能性.因此,即使WTSRegisterSessionNotification()成功,您也可能会丢失注册而无法实现.

试试这个:

interface

uses

...;

type

TfrmAlisson = class(TForm)

lbl2: TLabel;

protected

procedure CreateWnd; override;

procedure DestroyWindowHandle; override;

procedure WndProc(var Message: TMessage); override;

public

LockedCount: Integer;

end;

implementation

const

NOTIFY_FOR_THIS_SESSION = $0;

NOTIFY_FOR_ALL_SESSIONS = $1;

function WTSRegisterSessionNotification(hWnd: HWND; dwFlags: DWORD): Boolean; stdcall; external 'wtsapi32.dll' name 'WTSRegisterSessionNotification';

function WTSUnRegisterSessionNotification(hWnd: HWND): Boolean; stdcall; external 'wtsapi32.dll' name 'WTSUnRegisterSessionNotification';

procedure TfrmAlisson.CreateWnd;

begin

inherited;

if not WTSRegisterSessionNotification(Handle, NOTIFY_FOR_THIS_SESSION) then

RaiseLastOSError;

end;

procedure TfrmAlisson.DestroyWindowHandle;

begin

WTSUnRegisterSessionNotification(Handle);

inherited;

end;

procedure TfrmAlisson.WndProc(var Message: TMessage);

begin

if Message.Msg = WM_WTSSESSION_CHANGE then

begin

case Message.wParam of

WTS_SESSION_LOCK: begin

Inc(LockedCount);

end;

WTS_SESSION_UNLOCK: begin

lbl2.Caption := Format('Session was locked %d times.', [LockedCount]);

end;

end;

end;

inherited;

end;

end.

话虽这么说,考虑编写代码不依赖于VCL的窗口娱乐行为.您可以分配一个专用窗口来监视会话更改:

interface

uses

...;

type

TfrmAlisson = class(TForm)

lbl2: TLabel;

procedure FormCreate(Sender: TObject);

procedure FormDestroy(Sender: TObject);

private

SessionWnd: HWND;

procedure SessionWndProc(var Message: TMessage);

public

LockedCount: Integer;

end;

implementation

const

NOTIFY_FOR_THIS_SESSION = $0;

NOTIFY_FOR_ALL_SESSIONS = $1;

function WTSRegisterSessionNotification(hWnd: HWND; dwFlags: DWORD): Boolean; stdcall; external 'wtsapi32.dll' name 'WTSRegisterSessionNotification';

function WTSUnRegisterSessionNotification(hWnd: HWND): Boolean; stdcall; external 'wtsapi32.dll' name 'WTSUnRegisterSessionNotification';

procedure TfrmAlisson.FormCreate(Sender: TObject);

begin

SessionWnd := AllocateHWnd(SessionWndProc);

if not WTSRegisterSessionNotification(SessionWnd, NOTIFY_FOR_THIS_SESSION) then

RaiseLastOSError;

end;

procedure TfrmAlisson.FormDestroy(Sender: TObject);

begin

if SessionWnd <> 0 then

begin

WTSUnRegisterSessionNotification(SessionWnd);

DeallocateHWnd(SessionWnd);

end;

end;

procedure TfrmAlisson.SessionWndProc(var Message: TMessage);

begin

if Message.Msg = WM_WTSSESSION_CHANGE then

begin

case Message.wParam of

WTS_SESSION_LOCK: begin

Inc(LockedCount);

end;

WTS_SESSION_UNLOCK: begin

lbl2.Caption := Format('Session was locked %d times.', [LockedCount]);

end;

end;

end;

Message.Result := DefWindowProc(SessionWnd, Message.Msg, Message.WParam, Message.LParam);

end;

end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值