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.
现在的问题是消息可以正常截获,但是当把时间写回后程序就当掉了(时间是正确写回了),不知何故,请大家帮忙看看,先谢了!
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.