unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, MCForms, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Panel1: TPanel; procedure Button4Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private Grids: array[0..10,0..5] of TLabel; CurrPoint: TPoint; Created: Boolean; procedure UpdatePoint; procedure CreateGrid; public { 公共成员(变量、函数)声明 } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.UpdatePoint; begin if CurrPoint.x >= 5 then begin if CurrPoint.y >= 10 then Exit; CurrPoint.x := 0; Inc(CurrPoint.y); end else Inc(CurrPoint.x); end;procedure TForm1.CreateGrid; var I, J, FLeft, FTop, FWidth, FHeight : Integer; begin if Created then begin for I := 0 to 10 do for J := 0 to 5 do Grids[I,J].Color := clWhite; CurrPoint.x := 0; CurrPoint.y := 0; Exit; end; Created := True; FLeft := 5; FTop := 5; FWidth := (Panel1.Width - 5*6 - FLeft) div 6; FHeight := (Panel1.Height - 5*11 - FTop) div 11; for I := 0 to 10 do begin for J := 0 to 5 do begin Grids[I,J] := TLabel.Create(Self); with Grids[I,J] do begin AutoSize := False; Caption := EmptyStr; Left := FLeft; Top := FTop; Width := FWidth; Height := FHeight; Color := clWhite; Parent := Panel1; end; FLeft := FLeft + FWidth + 5; end; FTop := FTop + FHeight + 5; FLeft := 5; end; FillChar(CurrPoint,SizeOf(CurrPoint),0); end;procedure TForm1.Button4Click(Sender: TObject); begin CreateGrid; end;procedure TForm1.Button1Click(Sender: TObject); begin if Not Created then CreateGrid; Grids[CurrPoint.y,CurrPoint.x].Color := clRed; UpdatePoint; end;procedure TForm1.Button2Click(Sender: TObject); begin if Not Created then CreateGrid; Grids[CurrPoint.y,CurrPoint.x].Color := clGreen; UpdatePoint; end;procedure TForm1.Button3Click(Sender: TObject); begin if Not Created then CreateGrid; Grids[CurrPoint.y,CurrPoint.x].Color := clYellow; UpdatePoint; end;end.
可以用动态创建的Panel数组来实现。
http://community.csdn.net/Expert/topic/5517/5517898.xml?temp=.379101谢谢你的答案,还有个问题,怎么把 Button1,Button2,Button3和键盘的 数字键“1”,“2”,“3”联系起来,也就是按“1”键就执行Button1的按钮,按“2”键就执行Button2的按钮,按“3”键就执行Button3的按钮。procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); var btn: TComponent; begin if Key in ['1'..'3'] then begin btn := FindComponent('Button'+Key); if Assigned(btn) and (btn is TButton) then (btn as TButton).Click; end; end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MCForms, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Grids: array[0..10,0..5] of TLabel;
CurrPoint: TPoint;
Created: Boolean;
procedure UpdatePoint;
procedure CreateGrid;
public
{ 公共成员(变量、函数)声明 }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.UpdatePoint;
begin
if CurrPoint.x >= 5 then
begin
if CurrPoint.y >= 10 then Exit;
CurrPoint.x := 0;
Inc(CurrPoint.y);
end
else
Inc(CurrPoint.x);
end;procedure TForm1.CreateGrid;
var
I, J, FLeft, FTop, FWidth, FHeight : Integer;
begin
if Created then
begin
for I := 0 to 10 do
for J := 0 to 5 do
Grids[I,J].Color := clWhite;
CurrPoint.x := 0;
CurrPoint.y := 0;
Exit;
end; Created := True;
FLeft := 5;
FTop := 5;
FWidth := (Panel1.Width - 5*6 - FLeft) div 6;
FHeight := (Panel1.Height - 5*11 - FTop) div 11;
for I := 0 to 10 do
begin
for J := 0 to 5 do
begin
Grids[I,J] := TLabel.Create(Self);
with Grids[I,J] do
begin
AutoSize := False;
Caption := EmptyStr;
Left := FLeft;
Top := FTop;
Width := FWidth;
Height := FHeight;
Color := clWhite;
Parent := Panel1;
end;
FLeft := FLeft + FWidth + 5;
end;
FTop := FTop + FHeight + 5;
FLeft := 5;
end;
FillChar(CurrPoint,SizeOf(CurrPoint),0);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
CreateGrid;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if Not Created then CreateGrid;
Grids[CurrPoint.y,CurrPoint.x].Color := clRed;
UpdatePoint;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if Not Created then CreateGrid;
Grids[CurrPoint.y,CurrPoint.x].Color := clGreen;
UpdatePoint;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
if Not Created then CreateGrid;
Grids[CurrPoint.y,CurrPoint.x].Color := clYellow;
UpdatePoint;
end;end.
var
btn: TComponent;
begin
if Key in ['1'..'3'] then
begin
btn := FindComponent('Button'+Key);
if Assigned(btn) and (btn is TButton) then
(btn as TButton).Click;
end;
end;
设置Button1的Caption 为 Button1(&1)
设置Button1的Caption 为 Button2(&2)
设置Button1的Caption 为 Button1(&3)