Delphi动态创建组件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,stdctrls;
type
TForm2 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
public
//拦截Left mouse down 消息
procedure WMMOUSEDOWN(var Msg:TWMLButtonDown);message WM_LBUTTONDOWN;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
var
I: Integer;
begin
//释放创建的对象
for I := ComponentCount-1 downto 0 do
begin
if Components[i] is TButton then
begin
TButton(Components[i]).Free;
end;
end;
Action := cafree;
end;
procedure TForm2.WMMOUSEDOWN(var Msg: TWMLButtonDown);
var
button:TBUtton;
begin
if msg.msg = WM_LBUTTONDOWN then
begin
Button := TButton.Create(Form2);
button.Parent := Form2;
button.Left := msg.XPos;
button.Top := msg.YPos;
button.Width := button.Width + 50;
button.Caption := Format('button at %d,%d',[msg.XPos,msg.YPos]);
end;
end;
end.
posted on 2011-10-02 01:40 ManLoveGirls 阅读(1138) 评论(0) 编辑 收藏 举报