Delphi 在TPanel中随机生成行列的子Tpanel处理方法

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
 8 
 9 type
10   TForm1 = class(TForm)
11     Panel1: TPanel;
12     Button1: TButton;
13     Label1: TLabel;
14     Edit_Rows: TEdit;
15     Edit_Cols: TEdit;
16     Label2: TLabel;
17     Label3: TLabel;
18     procedure Button1Click(Sender: TObject);
19   private
20     { Private declarations }
21     function RetTPanel(Name: string; Width,Height: Integer): Tpanel;
22   public
23     { Public declarations }
24   end;
25 
26 var
27   Form1: TForm1;
28 
29 implementation
30 
31 {$R *.dfm}
32 
33 
34 procedure TForm1.Button1Click(Sender: TObject);
35 var
36   Bits: array of TPanel;
37   i,x,y,Rows,Cols,TpanelWidth,TpanelHeight: Integer;
38 begin
39   //3行 5列
40   Rows := StrToIntDef(edit_Rows.Text,2);
41   Cols := StrToIntDef(edit_Cols.Text,2);
42   TpanelWidth := Panel1.Width div Cols;
43   TpanelHeight := Panel1.Height div Rows;
44   SetLength(Bits,Rows*Cols);
45   for i := 0 to Length(Bits) - 1 do
46   begin
47     Bits[i] := RetTPanel('TPanel'+IntToStr(i),TpanelWidth,TpanelHeight);
48   end;
49 
50   { 绘制 }
51   x := 0;
52   y := 0;
53   for i := 0 to Length(Bits) - 1 do
54   begin
55     Bits[i].Parent := Panel1;
56     Bits[i].Left := x;
57     Bits[i].Top := y;
58 
59     Inc(x, Bits[i].Width);
60     if x >= Cols*Bits[i].Width then
61     begin
62       x := 0;
63       Inc(y, Bits[i].Height);
64     end;
65   end;
66 end;
67 
68 function TForm1.RetTPanel(Name: string; Width,Height: Integer): Tpanel;
69 begin
70   Result := Tpanel.Create(Application);
71   Result.Name := Name;
72   Result.Caption := Name;
73   Result.Color := clRed;
74   Result.Width := Width;
75   Result.Height := Height;
76   Result.Enabled := True;
77   Result.Visible := True;
78 end;
79 
80 end.

 

posted @ 2019-10-04 15:28  襄阳古城  阅读(547)  评论(0编辑  收藏  举报