如题,例如:我要设置Form的Caption但“Caption”这个属性我事先是不知道的,要从一个变量中取出来。这种情况我如何做呢?份不够可以再加.
解决方案 »
- 求助。。刚学习delphi 求一个dbgrid数据导出HTML文件的例子。要求HTML格式与excel格式一样!
- delphi flag值是怎么用的
- 动态链接库实现进程问题!~
- EXERCICE: 算法分析, 有密文和明文, 就当家庭作业吧, 嘿. . . . . .
- 我想在程序中用pascal 打开一个*.pas怎么弄啊
- OpenDialog组件,在线等待。。。。
- 如何在delphi中调用dos命令,比如telnet
- 这个问题邪门了,谁知道立马送200分
- 怎样在实现IE状态栏里的那个进度条?或是有没有这方面的控件?
- 求救,本人想写一个图片格式转换的程序,请问会用到什么函数
- 如何在程序是捕捉由flash控件打开的flash图形中的按扭的click事件,以及操作flash图形中的对象。
- 为什么在SQl sever里创建的数据库在DELPHI里找不到啊?
Function CreateAClass(Const AClassName:String):TObject;
var
C :TFormClass;
SomeObject :TObject;
begin
C :=TFormClass(FindClass(AClassName));
SomeObject :=C.Create(Nil);
Result :=SomeObject;
end;procedure GetBaseClassInfo(AClass :TObject;AStrings :TStrings);
var
ClassTypeInfo :PTypeInfo;
ClassTypeData :PTypeData;
EnumName:String;
begin
ClassTypeInfo :=AClass.ClassInfo;
ClassTypeData :=GetTypeData(ClassTypeInfo);
with AStrings do
begin
Add(Format('Class Name: %s',[ClassTypeInfo.Name]));
EnumName :=GetEnumName(TypeInfo(TTypeKind),Integer(ClassTypeInfo.Kind));
Add(Format('Kind: %s',[EnumName]));
Add(Format('Size: %d',[AClass.InstanceSize]));
Add(Format('Defined in %s.Pas',[ClassTypeData.UnitName]));
Add(Format('Num Properties %d',[ClassTypeData.PropCount]));
end;
end;
procedure GetClassProperties(AClass :TObject;AStrings :TStrings);
var
proplist :PpropList;
ClassTypeInfo :PTypeInfo;
ClassTypeData :PTypeData;
i:integer;
NumProps :Integer;
begin
ClassTypeInfo :=AClass.ClassInfo;
ClassTypeData :=GetTypeData(ClassTypeInfo);
if ClassTypeData.PropCount<>0 then
begin
GetMem(proplist,Sizeof(PpropList)*ClassTypeData.PropCount);
try
GetPropInfos(AClass.ClassInfo,proplist);
for i:=0 to ClassTypeData.PropCount-1 do
if proplist[i]^.PropType^.Kind<>tkMethod then
AStrings.Add(Format('%s: %s',[proplist[i]^.Name,proplist[i]^.PropType^.Name])); NumProps :=GetPropList(AClass.ClassInfo,[tkMethod],proplist);
AStrings.Add('');
AStrings.Add(' Event ====== ');
AStrings.Add(''); for i:=0 to NumProps do
AStrings.Add(format('%s: %s',[proplist[i]^.Name,proplist[i]^.propType^.Name])); finally
FreeMem(proplist,sizeof(Pproplist)*ClassTypeData.PropCount);
end;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
listbox3.Items.Add('TApplication');
listbox3.Items.Add('TButton');
listbox3.Items.Add('TForm');
listbox3.Items.Add('TListBox');
listbox3.Items.Add('TPaintBox');
listbox3.Items.Add('TTimer');
listbox3.Items.Add('TFindDialog');
listbox3.Items.Add('TOpenDialog');
listbox3.Items.Add('TComponent');
end;procedure TForm1.ListBox3Click(Sender: TObject);
var
somecomp:TObject;
begin
listbox1.Items.Clear;
listbox2.Items.Clear; somecomp :=CreateAClass(listbox3.Items[listbox3.itemindex]);
try
GetBaseClassInfo(somecomp,listbox1.Items);
//GetClassAncestry(somecomp,listbox1.Items);
GetClassProperties(somecomp,listbox2.Items);
finally
somecomp.Free;
end;
end;
大家有好办法都可以贴上来讨论。
自己看 $(delphi)\source\Rtl\Common\TypInfo.pas下面是一个简单的例子,是一些常用的函数和类型,可以直接用SetPropValue函数procedure SetCaptionProp(Instance : TObject; const s : string);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(Instance, 'caption');
if PropInfo = nil then //没有caption属性
exit;
if PropInfo^.SetProc = nil then //只读属性
exit;
if not (PropInfo^.PropType^^.Kind in [tkString, tkLString, tkWString]) then
exit; //类型不匹配 SetPropValue(Instance, 'caption', s);
end;