程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 通過網絡同步時間,Delphi代碼

通過網絡同步時間,Delphi代碼

編輯:Delphi

Delphi通過網絡同步時間的功能,功能方面類似於Windows系統時間設置面板中的同步服務器時間,這個界面有些粗糙哈,截圖如下所示:

Delphi通過網絡同步時間

Delphi同步網絡服務器時間代碼:

vIEw source print? 01 unit DTDem; 02 interface 03 uses 04   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 05   StdCtrls, Psock, NMDayTim, ExtCtrls, ComCtrls, ScktComp; 06 type 07   TForm1 = class(TForm) 08     Panel1: TPanel; 09     Edit1: TEdit; 10     Edit2: TEdit; 11     Label1: TLabel; 12     Label2: TLabel; 13     NMDayTime1: TNMDayTime; 14     Button1: TButton; 15     Label3: TLabel; 16     StatusBar1: TStatusBar; 17     procedure Button1Click(Sender: TObject); 18     procedure NMDayTime1Connect(Sender: TObject); 19     procedure NMDayTime1Disconnect(Sender: TObject); 20     procedure NMDayTime1HostResolved(Sender: TComponent); 21     procedure NMDayTime1Status(Sender: TComponent; Status: String); 22     procedure NMDayTime1ConnectionFailed(Sender: TObject); 23     procedure NMDayTime1InvalidHost(var handled: Boolean); 24   private 25     { Private declarations } 26   public 27     { Public declarations } 28   end; 29 const 30   StdCap = 'The Current Date and Time is : '; 31 var 32   Form1: TForm1; 33 implementation 34 {$R *.DFM} 35 procedure TForm1.Button1Click(Sender: TObject); 36 begin 37   NMDayTime1.ReportLevel := Status_Basic; 38 //  NMDayTime1.TimeOut := 300; 39   NMDayTime1.Host := Edit1.Text; 40   NMDayTime1.Port := StrToInt(Edit2.Text); 41   Label3.Caption := StdCap+NMDayTime1.DayTimeStr; 42 end; 43 procedure TForm1.NMDayTime1Connect(Sender: TObject); 44 begin 45   StatusBar1.SimpleText := 'Connected'; 46 end; 47 procedure TForm1.NMDayTime1Disconnect(Sender: TObject); 48 begin 49   If StatusBar1 <> nil then 50     StatusBar1.SimpleText := 'disconnected'; 51 end; 52 procedure TForm1.NMDayTime1HostResolved(Sender: TComponent); 53 begin 54   StatusBar1.SimpleText := 'Host resolved'; 55 end; 56 procedure TForm1.NMDayTime1Status(Sender: TComponent; Status: String); 57 begin 58   If StatusBar1 <> nil then 59     StatusBar1.SimpleText := status; 60 end; 61 procedure TForm1.NMDayTime1ConnectionFailed(Sender: TObject); 62 begin 63   ShowMessage('Connection Failed'); 64 end; 65 procedure TForm1.NMDayTime1InvalidHost(var handled: Boolean); 66 var 67   TmpStr: String; 68 begin 69   If InputQuery('Invalid Host!''Specify a new host:', TmpStr) then 70   Begin 71     NMDayTime1.Host := TmpStr; 72     Handled := TRUE; 73   End; 74 end; 75 end.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved