例子如下:
//Unit1 Code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure ButtonClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ButtonClick(Sender: TObject);
begin
TButton(Sender).Caption:='OnClick Event OK!';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
With TButton.create(Self) do
begin
parent:=Form1;
OnClick:=ButtonClick;
end;
end;
end.
//Form1
object Form1: TForm1
Left = 192
Top = 154
Width = 471
Height = 243
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 120
TextHeight = 16
object Button1: TButton
Left = 96
Top = 72
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end