How to detect system time change

If the user changes the system time either with Date and Time properties dialog box or using command line, we can detect this by catching WM_TIMECHANGE system message. Here is how.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    procedure WMTimeChange(var Msg: TMessage); message WM_TIMECHANGE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMTimeChange(var Msg: TMessage);
begin
  ShowMessage('System time was just changed!');
end;

end.

Leave a Reply