如何动态创建可视化控件并显示?
公司要求使用delphi,努力学习中。想自己做个扫雷游戏,其间想动态创建按钮数组,请问该如何实现?
我的代码如下:
unit UBtnTest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
btnTest: TButton;
{ Private declarations }
public
procedure NewInstant;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.NewInstant;
begin
btnTest:= TButton.Create(Form1);
btnTest.Left:= 0;
btnTest.Width:=50;
btnTest.Top:= 0;
btnTest.Height:= 25;
// btnTest.Show;
btnTest.Caption:= '开始';
btnTest.Visible:= true;
btnTest.Repaint;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NewInstant;
end;
end.
但是无法显示,望指点一二,不胜感激!
问题点数:20、回复次数:4Top
1 楼cuteant(我这张旧床票还能否登上你的破床|涛声是否依旧)回复于 2006-03-01 14:37:46 得分 8
给你个例子,你少了一句:Parent := Self;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TButton.Create(self) do
begin
Name := 'btnTest';
Caption := '测试';
Left := 0;
Top := 0;
Width := 100;
Height := 25;
Parent := Self;
end;
end;Top
2 楼cuteant(我这张旧床票还能否登上你的破床|涛声是否依旧)回复于 2006-03-01 14:38:49 得分 6
在你的程序里面加上btnTest.Parent := Self;就可以了Top
3 楼jadeluo(秀峰)回复于 2006-03-01 14:39:04 得分 6
procedure TForm1.NewInstant;
begin
btnTest:= TButton.Create(Self);
btnTest.Parent := Self; //你的代码少了这一句
btnTest.Left:= 0;
btnTest.Width:=50;
btnTest.Top:= 0;
btnTest.Height:= 25;
btnTest.Caption:= '开始';
btnTest.Visible:= True;
end;
Top
4 楼wvins(逸岚)回复于 2006-03-01 14:48:27 得分 0
总算成功了.
btnTest:= TButton.Create(self);
btnTest.Parent:= self;
不过要再加一个Button,
在FormCreate中调用似乎不行!Top




