程序大致如下:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
face=array[1..3,1..3] of TPanel;
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
GroupBox5: TGroupBox;
GroupBox6: TGroupBox;
Label1: TLabel;
Panel1: TPanel;
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
a,b,c,x,y,z:face;
implementation{$R *.dfm}var flag:integer;procedure TForm1.FormActivate(Sender: TObject);
var i,j:integer;
begin
for i:=1 to 3 do
for j:=1 to 3 do
begin
a[i,j]:=TPanel.Create(nil);
a[i,j].Parent:=form1.GroupBox1;
a[i,j].Top:=(i-1)*33+16;
a[i,j].Left:=(j-1)*33+9;
a[i,j].Width:=33;
a[i,j].Height:=33;
a[i,j].Color:=clred; b[i,j]:=TPanel.Create(nil);
b[i,j].Parent:=form1.GroupBox2;
b[i,j].Top:=(i-1)*33+16;
b[i,j].Left:=(j-1)*33+9;
b[i,j].Width:=33;
b[i,j].Height:=33;
b[i,j].Color:=clblue; c[i,j]:=TPanel.Create(nil);
c[i,j].Parent:=form1.GroupBox3;
c[i,j].Top:=(i-1)*33+16;
c[i,j].Left:=(j-1)*33+9;
c[i,j].Width:=33;
c[i,j].Height:=33;
c[i,j].Color:=cllime; x[i,j]:=TPanel.Create(nil);
x[i,j].Parent:=form1.GroupBox4;
x[i,j].Top:=(i-1)*33+16;
x[i,j].Left:=(j-1)*33+9;
x[i,j].Width:=33;
x[i,j].Height:=33;
x[i,j].Color:=clyellow; y[i,j]:=TPanel.Create(nil);
y[i,j].Parent:=form1.GroupBox5;
y[i,j].Top:=(i-1)*33+16;
y[i,j].Left:=(j-1)*33+9;
y[i,j].Width:=33;
y[i,j].Height:=33;
y[i,j].Color:=clblack; z[i,j]:=TPanel.Create(nil);
z[i,j].Parent:=form1.GroupBox6;
z[i,j].Top:=(i-1)*33+16;
z[i,j].Left:=(j-1)*33+9;
z[i,j].Width:=33;
z[i,j].Height:=33;
z[i,j].Color:=clwhite;
end;
end;end.现在我想给a,b,c,x,y,z这些数组写MouseDown事件,又要能区分是谁触发的...怎么写? 比如要想点击a[1,1]能显示"i am a[1,1]",点击b[3,2]能显示"i am b[3,2]"等等.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
face=array[1..3,1..3] of TPanel;
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
GroupBox5: TGroupBox;
GroupBox6: TGroupBox;
Label1: TLabel;
Panel1: TPanel;
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
a,b,c,x,y,z:face;
implementation{$R *.dfm}var flag:integer;procedure TForm1.FormActivate(Sender: TObject);
var i,j:integer;
begin
for i:=1 to 3 do
for j:=1 to 3 do
begin
a[i,j]:=TPanel.Create(nil);
a[i,j].Parent:=form1.GroupBox1;
a[i,j].Top:=(i-1)*33+16;
a[i,j].Left:=(j-1)*33+9;
a[i,j].Width:=33;
a[i,j].Height:=33;
a[i,j].Color:=clred; b[i,j]:=TPanel.Create(nil);
b[i,j].Parent:=form1.GroupBox2;
b[i,j].Top:=(i-1)*33+16;
b[i,j].Left:=(j-1)*33+9;
b[i,j].Width:=33;
b[i,j].Height:=33;
b[i,j].Color:=clblue; c[i,j]:=TPanel.Create(nil);
c[i,j].Parent:=form1.GroupBox3;
c[i,j].Top:=(i-1)*33+16;
c[i,j].Left:=(j-1)*33+9;
c[i,j].Width:=33;
c[i,j].Height:=33;
c[i,j].Color:=cllime; x[i,j]:=TPanel.Create(nil);
x[i,j].Parent:=form1.GroupBox4;
x[i,j].Top:=(i-1)*33+16;
x[i,j].Left:=(j-1)*33+9;
x[i,j].Width:=33;
x[i,j].Height:=33;
x[i,j].Color:=clyellow; y[i,j]:=TPanel.Create(nil);
y[i,j].Parent:=form1.GroupBox5;
y[i,j].Top:=(i-1)*33+16;
y[i,j].Left:=(j-1)*33+9;
y[i,j].Width:=33;
y[i,j].Height:=33;
y[i,j].Color:=clblack; z[i,j]:=TPanel.Create(nil);
z[i,j].Parent:=form1.GroupBox6;
z[i,j].Top:=(i-1)*33+16;
z[i,j].Left:=(j-1)*33+9;
z[i,j].Width:=33;
z[i,j].Height:=33;
z[i,j].Color:=clwhite;
end;
end;end.现在我想给a,b,c,x,y,z这些数组写MouseDown事件,又要能区分是谁触发的...怎么写? 比如要想点击a[1,1]能显示"i am a[1,1]",点击b[3,2]能显示"i am b[3,2]"等等.
for j:=1 to 3 do
begin
a[i,j]:=TPanel.Create(nil);
a[i,j].Parent:=form1.GroupBox1;
a[i,j].tag := 10000 + i * 100 + j ;
.........
//a[] 对应10000, B[]对应20000, C[]对应30000等。 点击事件的写法如下procedure TForm1.MyPanelClick(Sender : TObject);
begin
if (TPanel(Sender).Tag < 20000) then
showMessage('a['+ intToStr((tag-10000) div 100) + ',' + intToStr(tag mod 100) + '] is Click')
else if (TPanel(Sender).Tag < 30000) then
showMessage('b['+ intToStr((tag-20000) div 100) + ',' + intToStr(tag mod 100) + '] is Click')
......end;
for i:= 0 to ComponentCount-1 do
if Components[i] is TPanel then
begin
case TPanel(Components[i]).left ...
case TPanel(Components[i]).top ...
end;
for j:=1 to 3 do
begin
在mousedowm中可以这写
if sender is TPanel then
begin
showmessage('i am' + Tpanel(sender).name);
end;
仅供参考
for J:=1 to 3 do
begin
//你的创建Panel的代码
A[I,J]:=TPanel.Create(Nil);
A[I,J].Parent:=Form1.GroupBox1;
A[I,J].Top:=(I-1)*33+16;
A[I,J].Left:=(J-1)*33+9;
A[I,J].Width:=33;
A[I,J].Height:=33;
A[I,J].Color:=clRed;
//在Panel的Hint属性里面记录当前Panel在数组中的位置
A[I,J].Hint:=IntToStr(I)+','+IntToStr(J);
//将所创建的Panel的OnClick事件初始化
A[I,J].OnClick:=Form1.SomeClick;
//后面的代码类似处理
//........
end; procedure TForm1.SomeClick(Sender:TObject);
begin
ShowMessage('I am a ['+TPanel(Sender).Hint+']');
end;