//サンプル フォームをクリックした位置にボタンを生成しイベントを割り当てる
private
procedure ShowMsgBox(Sender :TObject);
---------------------------------------------------------------------------
procedure TForm1.ShowMsgBox(Sender :TObject);
var
S :string;
begin
S :=(Sender as TButton).Name;
MessageDlg('押されたのは' + S + 'です。',mtInformation,[MBOK], 0);
end;
var
Count :integer =1; //ボタンのCaption用
//フォーム上をクリックするとマウス位置にボタンを生成
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button =mbRight then Exit;
with TButton.Create(Self) do begin //生成時にオーナーを指定
Parent :=Self; //親ウィンドウを指定
Left :=X; //表示位置はマウスクリック位置
Top :=Y;
Width :=Width +Count * 2;
Name :=Format('PushButton%d', [Count]);
Caption :=Name;
OnClick :=Form1.ShowMsgBox; //イベントの動的割り当て
end;
Inc(Count);
end; |