我們經常看到許多網路下載的試用版軟體,都有使用時間的限制,就其商業角度而言也是處於軟體效益保護的一種措施,可以讓使用者免費試用一段時間,若滿意就可以購買商業軟體。本文所述實例程式碼功能就是如何為Delphi所寫的程式新增使用時間的限制功能,這裡預設的時限為30天。
主要程式碼如下:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Registry, Dialogs;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public Publicvar, public); Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);var registerTemp : TRegistry; curDate : TDateTime;begin registerTemp := TRegistry.Create; with registerTemp do begin Root := HKeyKEYCAL_MACHINE HKey; 'Software/MySoftware',True) then begin if ReadBool('Runned') then //不是第一次執行begin curDate := Date; if (curDate-ReadTime('LastRunTime'))>=ReadInteger('Duration') then begin //目前的系統時間超出了使用期限ShowMessage('試用版已到期'); exit; end else begin DeleteKey('LastRunTime'); WriteTime('LastRunTime',Date); end; end else begin //初次執行程式DeleteKey('Runned'); WriteBool('Runned',True); //設定試用期限30天WriteInteger ('Duration',30); //寫入目前運行時間WriteTime('LastRunTime',Date); end; end else begin ShowMessage('Fails!'); end; CloseKey; end;end;end.