动态表单生成.dfm以及关联.pas用于二次开发

本文介绍方法一、

新建一个工程文件,建form1,form2,其中form1加入一个按钮控件控件,

写事件如下

uses unit2;

procedure TForm1.Button1Click(Sender: TObject);

begin

     Form2.Show;//form2在工程文件中已创建 Application.CreateForm(TForm2, Form2);

     WriteComponentResFile('D:\unit2.dfm',form2);

end;

Form2中放置一个pagecontrol1,

写代码如下

unit Unit2;

interface

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;

type

  TForm2 = class(TForm)

    PageControl1: TPageControl;

    procedure FormCreate(Sender: TObject);

    procedure fButtonClick(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  private

    fButton:array of array of TButton;

    fnewpage:array of TTabSheet;

    fstr1,fstr2:array of array of String;

  public

    procedure ClearControls(AParent: TTabSheet);

  end;

var

  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.fButtonClick(Sender: TObject);

var

  s,s1,s2:string;

  i,j,x:Integer;

begin

    //

    s:= (Sender as TButton).Name;

    x:=pos('j',s);

    s1:=copy(s,4,x-4);

    s2:=copy(s,x+1,length(s)-x);

    i:=strtoint(s1);

    j:=strtoint(s2);

    showmessage(s);

end;

procedure TForm2.FormCreate(Sender: TObject);

const

  col2=5;

  lrAside=10;

  width1=160;

  height1=40;

  btnMagins=20;

var

  sc,sc1:string;

  rc,rd:Integer;

  i,j:Integer;

  st:String;

begin

     begin

        rc:=10;

        SetLength(fnewpage,rc);

        SetLength(fButton,rc);

        SetLength(fstr1,rc);

        SetLength(fstr2,rc);

     for I := 0 to rc-1 do begin

       fnewpage[I]:=TTabSheet.Create(pagecontrol1);

       sc1:='测试模块'+inttostr(I);

       fnewpage[I].Caption:=sc1;

       fnewpage[i].Name:='fsc'+inttostr(i);

       fnewpage[I].PageControl:= PageControl1;

       PageControl1.ActivePage:=fnewpage[I];

       rd:=20;

       SetLength(fButton[I],rd);

       SetLength(fstr1[I],rd);

       SetLength(fstr2[I],rd);

       for J := low(fButton[I]) to high(fButton[I]) do

       begin

          fButton[i,j]:=TButton.Create(self);

          fButton[i,j].Parent:=fnewpage[i];

          fButton[i,j].Caption:='测试按钮('+inttostr(I)+':'+inttostr(J)+')';

          fButton[i,j].Name:='fcx'+inttostr(i)+'j'+inttostr(j);

          fButton[i,j].top:= ((J) div col2) * (height1+ lrAside) + lrAside ;

          fButton[i,j].Left:=((J) mod col2) * (width1 + btnMagins) + lrAside;

          fButton[i,j].Width:=width1;

          fButton[i,j].Height:=height1;

          st:='test'+ inttostr(I)+'j'+inttostr(J) ;

          fButton[i,j].hint:=st;

          fButton[i,j].OnClick:=fButtonClick;

       end;

     end;

     end;

end;

procedure TForm2.FormDestroy(Sender: TObject);

var

  I:Integer;

begin

    for I :=High(fnewpage) downto Low(fnewpage)  do

    begin

      ClearControls(fnewpage[i]);

      fnewpage[i].Free;

    end;

end;

procedure TForm2.ClearControls(AParent: TTabSheet);

var

  i: Integer;

begin

  for i := AParent.ControlCount - 1 downto 0 do begin

    if not AParent.Controls[i].InheritsFrom(TWinControl) then Continue;

    try

      if TWinControl(AParent.Controls[i]).ControlCount > 0 then begin

        ClearControls(TTabSheet(AParent.Controls[i]));

      end;

      AParent.Controls[i].Free;

    except

    end;

  end;

end;

end.

运行form1,按下form1的按钮控件,会在d:\根目录地下生成unit2.dfm,只有.dfm文件没有关联的pas怎么办。拷贝上面的.pas文件,代码删除掉动态生成控件的部分,如下所示:

unit Unit2;

interface

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;

type

  TForm2 = class(TForm)

    PageControl1: TPageControl;

    procedure FormCreate(Sender: TObject);

    procedure fButtonClick(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  private

  public

  end;

var

  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.fButtonClick(Sender: TObject);

var

  s,s1,s2:string;

  i,j,x:Integer;

begin

    //

    s:= (Sender as TButton).Name;

    x:=pos('j',s);

    s1:=copy(s,4,x-4);

    s2:=copy(s,x+1,length(s)-x);

    i:=strtoint(s1);

    j:=strtoint(s2);

    showmessage(s);

end;

procedure TForm2.FormCreate(Sender: TObject);

begin

    //

end;

procedure TForm2.FormDestroy(Sender: TObject);

begin

   //

end;

end.

此时的form2是如下表单模样

在新建的工程文件中打开这个unit2.pas和unit2.dfm,就可以正常设计和修改了

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值