拦截WM_TimeChange消息修改系统时间的问题 ( 积分: 200 )

  • 主题发起人 主题发起人 mysirius
  • 开始时间 开始时间
M

mysirius

Unregistered / Unconfirmed
GUEST, unregistred user!
目的是在程序中拦截WM_TimeChange消息,如果时间被错误更改的话就将系统时间写回正确值。
现在的问题是消息可以正常截获,但是当把时间写回后程序就当掉了(时间是正确写回了),不知何故,请大家帮忙看看,先谢了!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
protected
procedure WndProc(var Msg: TMessage);override;
end;

var
Form1: TForm1;
SystemTime :TDateTime;
implementation

{$R *.dfm}

procedure TForm1.WndProc(var Msg:TMessage);
var
ADateTime :TSystemTime;
yy,mon,dd,hh,min,ss,ms :Word;
begin
if Msg.Msg=WM_TimeChange then
begin
DecodeDate(SystemTime ,yy,mon,dd);
DecodeTime(SystemTime ,hh,min,ss,ms);
With ADateTime Do
begin
wYear:=yy;
wMonth:=mon;
wDay:=dd;
wHour:=hh;
wMinute:=min;
wSecond:=ss;
wMilliseconds:=ms;
end;
SetLocalTime(ADateTime);
PostMessage(HWND_BROADCAST,WM_TIMECHANGE,0,0);
end;
inherited WndProc(Msg);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
SystemTime:=now;
end;
end.
 
应该是死循环了吧
 
procedure TForm1.WndProc(var Msg:TMessage);
var
ADateTime :TSystemTime;
yy,mon,dd,hh,min,ss,ms :Word;

function TimeChanged: Boolean;
begin
GetLocalTime(ADateTime);
Result := (ADateTime.wYear <> yy) or (ADateTime.wMonth <> mon) or
(ADateTime.wDay <> dd) or (ADateTime.wHour <> hh) or
(ADateTime.wMinute <> min) or (ADateTime.wSecond <> ss);
end;
begin
if (Msg.Msg=WM_TimeChange) then
begin
DecodeDate(SystemTime ,yy,mon,dd);
DecodeTime(SystemTime ,hh,min,ss,ms);
if TimeChanged then
begin
With ADateTime Do
begin
wYear:=yy;
wMonth:=mon;
wDay:=dd;
wHour:=hh;
wMinute:=min;
wSecond:=ss;
wMilliseconds:=ms;
end;
SetLocalTime(ADateTime);
PostMessage(HWND_BROADCAST,WM_TIMECHANGE,0,0);
end;
end;
inherited WndProc(Msg);
end;
 
两位说的没错,应该判断下截获的消息是不是自己设置时间时广播出去的,否则就会进入死循环。
 
后退
顶部