根據窗體名稱動態建立窗體

2021-04-25 17:44:11 字數 2828 閱讀 9903

unit unit1;

inte***ce

uses

windows, messages, sysutils, variants, classes, graphics, controls, forms,

dialogs, stdctrls, buttons, comctrls, db, adodb;

type

tform1

=class(tform)

treeview1: ttreeview;

bitbtn1: tbitbtn;

adoconnection1: tadoconnection;

adoquery1: tadoquery;

procedure

formcreate(sender: tobject);

procedure

formclose(sender: tobject;

varaction: tcloseaction);

procedure

treeview1dblclick(sender: tobject);

private

public

end;

tfrmname

=^mfrmname;

mfrmname

=record

ftype: string;

end;

varform1: tform1;

implementation

uses unit2, unit3;

const  //也就是說要預存了下面這兩個陣列才可以很好的實現該功能

arrformclass :

array[0

..1] of

tformclass

=(tfrmhumanresource,tfrmfinance);

arrclassname :

array[0

..1] of

string=(

'frmhumanresource',

'frmfinance');

function

createform(

const

anyform:tformclass):tform;

begin

result :

=end

;function

getindex(sstr : string) : integer;

vari :integer;

begin

result :=-

1;fori :

=low(arrclassname)

tohigh(arrclassname)

doif

sstr

=arrclassname[i]

then

begin

result :

=i ;

exit;

end;

end;

procedure

tform1.formcreate(sender: tobject);

varp:tfrmname;

nodea,nodeb:ttreenode;

begin

with

adoquery1

dobegin

nodea:

=treeview1.items.addchild(

nil,adoquery1.fieldvalues[

'frmcaption

']);

while

noteof

dobegin

new(p);

nodeb:

=treeview1.items.addchild(nodea,adoquery1.fieldvalues[

'frmcaption

']);

p^.ftype:

=adoquery1.fieldvalues[

'frmname'];

nodeb.data:=p;

next;

end;

end;

end;

procedure

tform1.formclose(sender: tobject;

varaction: tcloseaction);

vari:integer;

begin

fori :=0

totreeview1.items.count -1

doif

assigned(treeview1.items[i].data)

then

begin

dispose(tfrmname(treeview1.items[i].data));

treeview1.items[i].data   :

=nil

;end

;end

;procedure

tform1.treeview1dblclick(sender: tobject);

variindex : integer;

begin

ifassigned(treeview1.selected.data)  

then

begin

iindex :

=getindex(tfrmname(treeview1.selected.data).ftype);

ifiindex =-

1then

exit;

createform(arrformclass[iindex]);

end;

end;

end.

DELPHI動態建立窗體

第一種方式 procedure tform1.btn1click sender tobject begin try showmodal finally free end end 第二種方式 procedure tform1.btn2click sender tobject begin if not ...

動態建立Frame窗體(根據類名,除T以外的字母)

假設有如下frame窗體 fromframe,levelframe 一 如果需要根據類名動態生成它們,先在各frame窗體的pas中註冊它們各自的類。如下 initialization registerclass tfromframe initialization registerclass tle...

動態載入窗體 接上

public static class dynamiccreate if flag public static object dllinvoke string mdllfilename,string mnamespace,string mclassname,object mobjarrayparam...