可以,例如:<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure FormActivate(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> procedure ActionLog(showtime:bool;msg:string);<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure Tform1.ActionLog(showtime:bool;msg:string);<br>var<br>filename:string;<br>logfile:Textfile;<br>begin<br>filename:=changefileext(application.ExeName,'.log');<br>assignfile(logfile,filename);<br>if FileExists(filename) then<br>append(logfile)<br>else<br>rewrite(logfile);<br>if(showtime) then<br>writeln(logfile,datetimetostr(now)+':'+msg)<br>else<br>writeln(logfile,msg);<br>closefile(logfile);<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>ActionLog(true,Tbutton(sender).Name+'('+Tbutton(sender).Caption+')'+' Click!');<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>ActionLog(true,'Form Closed!');<br>ActionLog(false,'--------------------------------------------------------------')<br>end;<br><br>procedure TForm1.FormActivate(Sender: TObject);<br>begin<br>ActionLog(true,'Form Activate!');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>ActionLog(true,'Form Create!');<br>end;<br><br>end.<br>