我自己写过一个仿delphi,里面创建是这样的:
GetClassPropertiesGrid(tmpcompon,Gridproperty);
//GetClassPropertiesGrid函数的第一个参数是你的组件,
比如edit1,Gridproperty是你显示的StringGrid,我是显示在StringGrid当中.
你只要知道rtti,编程还是很好写的.
如果需要完整的程序:qq:153109
procedure Tform1.GetClassPropertiesGrid(AClass: TObject;var AStrings: TStringGrid);
var
PropList: PPropList;
ClassTypeInfo: PTypeInfo;
ClassTypeData: PTypeData;
i,tmpint: integer;
tmpstr:string;
tmpobj:Tobject;
begin
ClassTypeInfo := AClass.ClassInfo;
ClassTypeData := GetTypeData(ClassTypeInfo);
if ClassTypeData.PropCount <> 0 then
begin
GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
try
GetPropInfos(AClass.ClassInfo, PropList);
AStrings.RowCount:=ClassTypeData.PropCount;
tmpint:=0;
for i := 0 to ClassTypeData.PropCount - 1 do
begin
tmpstr:='';
case PropList^.PropType^.Kind of
tkMethod:continue;
tkInteger,tkInt64,tkEnumeration:tmpstr:=GetEnumProp(AClass,PropList^.Name);
tkFloat:tmpstr:=floattostr(GetFloatProp(AClass,PropList^.Name));
tkString,tkLString,tkWString:tmpstr:=GetStrProp(AClass,PropList^.Name);
tkClass:begin
tmpobj:=GetObjectProp(AClass,PropList^.Name);
if (tmpobj<>nil) then
begin
if (tmpobj is Tcomponent) then
tmpstr:=Tcomponent(tmpobj).Name
else
tmpstr:='('+Tcomponent(tmpobj).ClassName+')';
end;
end;
end;
AStrings.Cells[0,i]:=PropList^.Name;
if tmpstr<>'' then
AStrings.Cells[1,i]:=tmpstr;
inc(tmpint);
end;
AStrings.RowCount:=tmpint;
finally
FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
end;
end;
end;