UTimStop.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:5k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit UTimStop;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   DsgnIntf;
  6. const
  7.   DEF_MAX = 50;
  8.   DEF_AUTOSAVE = True;
  9.   DEF_AUTOLOAD = True;
  10.   DEF_ALERTMESSAGE = True;
  11.   DEF_MESSAGETEXT = 'You have reached the limit of times you can run this application. You cannot use it anymore until you register.';
  12.   DEF_KEY = 'SoftwareMy Program';
  13.   DEF_SECTION = 'Used Times Stop';
  14.   DEF_STOPEXECUTE = True;
  15. type
  16.   TAboutUsedTimesStop = class(TPropertyEditor)
  17.   public
  18.     procedure Edit; override;
  19.     function GetAttributes: TPropertyAttributes; override;
  20.     function GetValue: string; override;
  21.   end;
  22.   TUsedTimesStop = class(TComponent)
  23.   private
  24.     FMax: integer;
  25.     FAlert: Boolean;
  26.     FAutoSave: boolean;
  27.     FOnUsedAll: TNotifyEvent;
  28.     FUsed: integer;
  29.     FAbout: TAboutUsedTimesStop;
  30.     FKey: string;
  31.     FSection: string;
  32.     FMessageText: string;
  33.     FStopExecute: boolean;
  34.     procedure SetMax(Value: integer);
  35.     procedure SetAlert(Value: Boolean);
  36.   protected
  37.     procedure Loaded; override;
  38.   public
  39.     constructor Create(AOwner: TComponent); override;
  40.     destructor Destroy; override;
  41.     procedure ResetCounter;
  42.     procedure LockCounter;
  43.     function Load: boolean;
  44.     function Save: boolean;
  45.     property TimesUsed: integer read FUsed;
  46.   published
  47.     property About: TAboutUsedTimesStop read FAbout write FAbout;
  48.     property AlertMessage: Boolean read FAlert write SetAlert;
  49.     property AutoSave: boolean read FAutoSave write FAutoSave;
  50.     property MessageText: string read FMessageText write FMessageText;
  51.     property Max: integer read FMax write SetMax;
  52.     property OnUsedAll: TNotifyEvent read FOnUsedAll write FOnUsedAll;
  53.     property SaveKey: string read FKey write FKey;
  54.     property SaveSection: string read FSection write FSection;
  55.     property StopExecute: boolean read FStopExecute write FStopExecute;
  56.   end;
  57. procedure Register;
  58. implementation
  59. uses Registry;
  60. procedure TAboutUsedTimesStop.Edit;
  61. begin
  62.   Application.MessageBox('TUsedTimesStop component v1.00 for Delphi 2.0. Copyright (C) 1997 Ivan Azic. This component is freeware. For more details, see readme.doc file.',
  63.                          'About TUsedTimesStop Component', MB_OK + MB_ICONINFORMATION);
  64. end;
  65. function TAboutUsedTimesStop.GetAttributes: TPropertyAttributes;
  66. begin
  67.   Result:= [paMultiSelect, paDialog, paReadOnly];
  68. end;
  69. function TAboutUsedTimesStop.GetValue: string;
  70. begin
  71.   Result:= '(about)';
  72. end;
  73. procedure TUsedTimesStop.SetMax(Value: Integer);
  74. begin
  75.   FMax:= Value;
  76. end;
  77. procedure TUsedTimesStop.SetAlert(Value: boolean);
  78. begin
  79.   FAlert:= Value;
  80. end;
  81. constructor TUsedTimesStop.Create(AOwner: TComponent);
  82. begin
  83.   inherited Create(AOwner);
  84.   FMax:= DEF_MAX;
  85.   FAlert:= DEF_ALERTMESSAGE;
  86.   FMessageText:= DEF_MESSAGETEXT;
  87.   FKey:= DEF_KEY;
  88.   FSection:= DEF_SECTION;
  89.   FAutoSave:= DEF_AUTOSAVE;
  90.   FStopExecute:= DEF_STOPEXECUTE;
  91. end;
  92. destructor TUsedTimesStop.Destroy;
  93. begin
  94.   if not(csDesigning in ComponentState) then
  95.     if FAutoSave then
  96.       Save;
  97.   inherited Destroy;
  98. end;
  99. procedure TUsedTimesStop.Loaded;
  100. begin
  101.   inherited Loaded;
  102.   if not(csDesigning in ComponentState) then
  103.     if not(Load) then exit;
  104.   if (FUsed = -1) or (FUsed = 0) then exit;
  105.   if FUsed > FMax then begin
  106.     if FAlert then begin
  107.       Beep;
  108.       ShowMessage(FMessageText);
  109.     end;
  110.     if Assigned(FOnUsedAll) then
  111.       FOnUsedAll(Self);
  112.     if FStopExecute then
  113.       Halt;
  114.   end;
  115.   FUsed:= FUsed + 1;
  116.   if FAutoSave then
  117.     Save;
  118. end;
  119. function TUsedTimesStop.Load: boolean;
  120. var
  121.   p: TRegIniFile;
  122. begin
  123.   Result:= False;
  124.   if (FKey = '') or (FSection = '') then exit;
  125.   p:= TRegIniFile.Create(FKey);
  126.   try
  127.     FUsed:= p.ReadInteger(FSection, 'TimesUsed', 1);
  128.     Result:= True;
  129.   finally
  130.     p.Free;
  131.   end;
  132. end;
  133. function TUsedTimesStop.Save: boolean;
  134. var
  135.   p: TRegIniFile;
  136. begin
  137.   Result:= False;
  138.   if (FKey = '') or (FSection = '') then exit;
  139.   p:= TRegIniFile.Create(FKey);
  140.   try
  141.     p.EraseSection(FSection);
  142.     p.WriteInteger(FSection, 'TimesUsed', FUsed);
  143.     Result:= True;
  144.   finally
  145.     p.Free;
  146.   end;
  147. end;
  148. procedure TUsedTimesStop.ResetCounter;
  149. begin
  150.   FUsed:= 1;
  151.   if FAutoSave then
  152.     Save;
  153. end;
  154. procedure TUsedTimesStop.LockCounter;
  155. begin
  156.   FUsed:= -1;
  157.   if FAutoSave then
  158.     Save;
  159. end;
  160. procedure Register;
  161. begin
  162.   RegisterComponents('Samples', [TUsedTimesStop]);
  163.   RegisterPropertyEditor(TypeInfo(TAboutUsedTimesStop), TUsedTimesStop, 'ABOUT', TAboutUsedTimesStop);
  164. end;
  165. end.