一、先写一个TMyTimer类吧
unit U_MyTimer;
interface
uses Classes,ExtCtrls,SysUtils;
type
TMyTimer=class(TTimer)
private
FFileName,FPrefix:String;
procedure FOnTimer(Sender:TObject);
public
constructor Create(AOwner:TComponent;AInterval:Integer;AFileName,APrefix:string);
destructor Destroy;override;
//WriteDebugInfo 只是演示 MyTimer 一直在调用的过程
class procedure WriteDebugInfo(strFileName,strMsg: string);
end;
implementation
{ TMyTimer }
constructor TMyTimer.Create(AOwner: TComponent; AInterval:Integer;AFileName,APrefix:string);
begin
inherited Create(AOwner);
Interval:=AInterval;
OnTimer:=FOnTimer;
//以下两个参数只是为了看演示效果
FFileName:=AFileName;
FPrefix:=APrefix;
end;
destructor TMyTimer.Destroy;
begin
OnTimer:=nil;
inherited;
end;
procedure TMyTimer.FOnTimer(Sender: TObject);
begin
WriteDebugInfo(FFileName,FPrefix);
end;
class procedure TMyTimer.WriteDebugInfo(strFileName,strMsg: string);
var
f: textfile;
strLogFile, strLogDir: string;
begin
AssignFile(f, strFileName);
if not fileexists(strFileName) then
Rewrite(f)
else
Append(f);
try
Writeln(f, TimeToStr(now) + ' ' + strMsg);
finally
CloseFile(f);
end;
end;
end.
二、写dll
library MyTimerDll;
uses
SysUtils,
Classes,
U_MyTimer in 'U_MyTimer.pas';
{$R *.res}
//创建一个返回TMyTimer类型的函数
function MyTimerObj(AOnwer:TComponent;AInterval:Integer;AFileName,APrefix:String):TMyTimer;
begin
Result:=TMyTimer.create(AOnwer,AInterval,AFileName,APrefix);
end;
exports
MyTimerObj;
begin
end.
三、测试程式
unit U_Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
U_MyTimer;
//use u_MyTimer单元
//不然找不到 MyTimerObj函数的返回类型
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
btnTestMyTimer: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure btnTestMyTimerClick(Sender: TObject);
private
{ Private declarations }
FTimer1,FTimer2,FMyTimer:TMyTimer;
public
{ Public declarations }
end;
var
Form1: TForm1;
//声明Dll
function MyTimerObj(AOnwer:TComponent;AInterval:Integer;AFileName,APrefix:String):TMyTimer;external 'MyTimerDll.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Assigned(FTimer1) then
FTimer1:=TMyTimer.Create(Self,1000,'c:/Timer1.txt','TestTimer1');
FTimer1.Enabled:=not FTimer1.Enabled;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if not Assigned(FTimer2) then
FTimer2:=TMyTimer.Create(Self,333,'c:/Timer2.txt','TestTimer2');
FTimer2.Enabled:=not FTimer2.Enabled;
end;
procedure TForm1.btnTestMyTimerClick(Sender: TObject);
begin
//这里是真正调用dll中的函数哦,上面两个ButtonClick事件是为了和此函数比较调用方法
if not Assigned(FMyTimer) then
FMyTimer:=MyTimerObj(Self,250,'c:/MyTimerDll.txt','MyTimerDll');
FMyTimer.Enabled:=not FMyTimer.Enabled;
end;
end.