如何用delphi做组态软件!我在窗体上中动态(注意是动态)的创建了100个按扭,现在我想知道我单击其中任意一个。。。。。(100分)

Z

zyf23

Unregistered / Unconfirmed
GUEST, unregistred user!
我在窗体上中动态(注意是动态)的创建了100个按扭,现在我想知道在我创建这些按钮之后
我单击其中任意一个按扭时如何知道他的名字或他的其他属性!注意是在运行期!

我的实现思路是:
procedure TForm2.Button1Click(Sender: TObject);
var
button:tbutton;
i:integer;
begin
for i:= 3 to 100 do
begin
button:=tbutton.Create(self);
button.parent:=form2;
button.Left:=random(500);
button.top:=random(500);
button.Width:=10;
button.height:=10;
button.Font.Charset:=GREEK_CHARSET;
button.Font.Height:=-10;
button.ShowHint:=true;
button.tag:=i;
button.Hint:=inttostr(button.tag);
button.name:='button'+inttostr(i);
button.onClick:=button2.OnClick;
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
showmessage(sender.ClassName);//目前我只能知道是那一类发的click事件,
我无法准确的获取所发按扭的确切名字
end;

希望的到高手门的帮助!·
100分不够的话,我还有480分,这可是身价呀!
 
if button.tag = i then
 
ShowMessage(TButton(Sender).Name)
 
用FindVCLWindow這個函數!該函數返回窗口控件
FindVCLWindow(FindWindowControl(mouse.CursorPos))
 
让这100按钮共用一个过程,在这个过程中写

for i:=0 to ComponentCount-1 do
begin
if compontents is Tbutton then
begin
aaa:=Components.caption
end;
end;
 
我已经编译通过,如下:
procedure TForm1.Button1Click(Sender: TObject);
var
Button:TButton;
I:Integer;
begin
for I:= 3 to 100 do
begin
Button:=TButton.Create(self);
Button.parent:=form1;
Button.Left:=random(500);
Button.top:=random(500);
Button.Width:=75;
Button.height:=25;
Button.Font.Charset:=GREEK_CHARSET;
Button.Font.Height:=-10;
Button.ShowHint:=true;
Button.tag:=i;
Button.Hint:=inttostr(button.tag);
Button.name:='button'+inttostr(i);
Button.onClick:=XXX;
end;
end;

procedure TForm1.XXX(Sender: TObject);
begin
ShowMessage('Button'+Self.ActiveControl.Hint);//关键句在这,Self.ActiveControl
//即获得焦点的控件;
end;
如果合适,请给点分。
 
忘了,函数XXX,实际上与.Button2Click一样,
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure XXX(Sender: TObject)
//在这;
private
{ Private declarations }
public
{ Public declarations }
end;
 
procedure TForm1.MyButtonClick(Sender: TObject);
begin
Edit1.Text :=(Sender as TButton).ClassName;
end;

procedure TForm1.CreateButton;
begin
Button:=TButton.Create(self);
Button.parent:=form1;
Button.Left:=random(500);
Button.top:=random(500);
Button.Width:=75;
Button.height:=25;
Button.Font.Charset:=GREEK_CHARSET;
Button.Font.Height:=-10;
Button.ShowHint:=true;
Button.tag:=1000;
Button.Hint:=inttostr(button.tag);
Button.name:='button'+inttostr(i);
Button.onClick:=MyButtonClick;//在这里
end;
 
to :御键飞天,
拜托再想想是什么函数,你的第一中我一试过,可以,但是和我要实现的
下一步工作有点不协调!
:fxWSY,
你的也可以,但我现在的问题是这些动态创建的按钮,我想在运行起使用鼠标移动
他们到合适的位置,不知有什么好的方法没?

:ArJianzeng,
你的和我的一样,非常感谢!

谁用这方面的资料,源程序!

分数不成问题!

我的email:
10m: zyf23@163.net
30M: dayhillbird@sdzg.com(企业级)
 
procedure TForm1.MyButtonClick(Sender: TObject);
var
s: string;
begin
s := Format('您单击了第 %d 个按钮,他的横坐标是 %d',
[(Sender as TButton).Tag, (Sender as TButton).Left]);
ShowMessage(s);
end;
 
搞不懂了,你不是要每个按钮的名称吗?不是已经实现了,ArJianzeng那样获得的是类名.
到底想要什么?
 
移动button控件
procedure TForm1.button2MouseDown(Sender: TObject
Button: TMouseButton;
Shift: TShiftState
X, Y: Integer);
begin
releasecapture;
edit1.Perform(wm_syscommand,$f012 ,0);
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
showmessage(Tbutton(sender0.Name);
end;
procedure TForm2.Button1Click(Sender: TObject);
var
button:tbutton;
i:integer;
begin
for i:= 3 to 100 do
begin
button:=tbutton.Create(self);
button.parent:=form2;
button.Left:=random(500);
button.top:=random(500);
button.Width:=10;
button.height:=10;
button.Font.Charset:=GREEK_CHARSET;
button.Font.Height:=-10;
button.ShowHint:=true;
button.tag:=i;
button.Hint:=inttostr(button.tag);
button.name:='button'+inttostr(i);
button.onClick:=button2.OnClick;
button.onmousedown:=button2.onmousedown
end;
end;

 
这样子用是肯定不行的。
如果你细心一点,相信你可以发现你的button实际上是一个局部变量声明。所以。。。。。。
呵呵,你的100个Button实际上只有最后一个保留了,其他的都是动态创建完了立即就释放
了(Delphi自动为你做的)。
你可以试一试下面这个办法:
var
ArrayBtn:Array[1..100] of TButton;//将该声明作为全局声明或者窗体类的一个域
然后就可以执行你上面的代码了。
如果想进一步获取这些按钮的信息。你可以通过判断(Sender as TButton).tag 的值来
定位某一个按钮,就像你在创建他们时一样。
 
顶部