我們經(jīng)常看到很多網(wǎng)上下載的試用版軟件,都有使用時(shí)間的限制,就其商業(yè)角度而言也是處于軟件效益保護(hù)的一種措施,可以讓用戶免費(fèi)試用一段時(shí)間,若滿意就可以購買商業(yè)軟件。本文所述實(shí)例代碼功能就是如何為Delphi所編寫的程序添加使用時(shí)間的限制功能,這里默認(rèn)的時(shí)限為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 { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);var registerTemp : TRegistry; curDate : TDateTime;begin registerTemp := TRegistry.Create; with registerTemp do begin RootKey := HKEY_LOCAL_MACHINE; //判斷是否初次運(yùn)行程序 if OpenKey('Software/MySoftware',True) then begin if ReadBool('Runned') then //不是第一次運(yùn)行 begin curDate := Date; if (curDate-ReadTime('LastRunTime'))>=ReadInteger('Duration') then begin //當(dāng)前的系統(tǒng)時(shí)間超出了使用期限 ShowMessage('試用版已到期'); exit; end else begin DeleteKey('LastRunTime'); WriteTime('LastRunTime',Date); end; end else begin //初次運(yùn)行程序 DeleteKey('Runned'); WriteBool('Runned',True); //設(shè)置試用期限30天 WriteInteger('Duration',30); //寫入當(dāng)前運(yùn)行時(shí)間 WriteTime('LastRunTime',Date); end; end else begin ShowMessage('Fails!'); end; CloseKey; end;end;end.
新聞熱點(diǎn)
疑難解答
圖片精選