help !(50分)

  • 主题发起人 主题发起人 poy
  • 开始时间 开始时间
P

poy

Unregistered / Unconfirmed
GUEST, unregistred user!
有没有办法用循环,把一个有多个属性的对像的所有属性名称和值取出来?
Thx !!!
 
《DELPHI5开发人员指南》上有例子的
 
我现在没有这本书,help
 
:)代码比较长。
 
有人会吗?有高手吗?
 
uses TypInfo

///get PropList
var
pProps: PPropList;
nTotProps, nProps, I: Integer;
begin
//get the total number of properties
nTotProps := GetTypeData(ClassInfo).PropCount;
//allocate the required memory
GetMem(pProps, sizeof(PPropInfo) * nTotProps);
try
//fill the pProps with a filtered list
nProps := GetPropList(ClassInfo, [tkEnumeration], pProps);
finally
FreeMem(pProps, sizeof(PPropInfo) * nTotProps);
end;



////get prop value sample string property
var
PropInfo: PPropInfo;
PropInfo := GetPropInfo( aTButton.ClassInfo, aTEdit.Text);
if PropInfo <> nil then
if PropInfo^.PropType^.Kind = tkLString then
Caption := GetStrProp(aTButton, PropInfo)
else ShowMessage('Not a string property')
else
showMessage('Property doesn''t exist');
 
Thx to wlmmlw ,用你的方法又碰到新的问题 :
我把你的代码改成(如下),运行之后很多在属性框中没有的属性出来了,
我只想显示属性框中的属性和其值,不论其值类型是字符还是数值(用你的
代码只能获取String型的数值 ) ,这要咱办?:
procedure TForm1.aTButtonClick(Sender: TObject);
var
pProps: PPropList;
nTotProps, nProps, I: Integer;
StrList :TStringList;
begin
//get the total number of properties
nTotProps := GetTypeData(ClassInfo).PropCount;
//allocate the required memory
GetMem(pProps, sizeof(PPropInfo) * nTotProps);
try
//fill the pProps with a filtered list
nProps := GetPropList(ClassInfo, [tkUnknown, tkChar, tkEnumeration,
tkString, tkSet, tkClass, tkWChar, tkLString, tkWString,
tkVariant, tkArray, tkRecord, tkInterface, tkDynArray], pProps);
StrList :=TStringList.Create;
for i :=0 to nTotProps-1 do
begin
try
GetPropertyToList(aTButton,pProps^,StrList,true);
except
continue ;
end;
end;
ListBox1.Items :=StrList;
Caption := IntTostr(ListBox1.Items.count)
finally
FreeMem(pProps, sizeof(PPropInfo) * nTotProps);
end;
end;

procedure TForm1.GetPropertyToList(Instance: TObject; PropInfo: PPropInfo;
var rStrList:TStringList;NeedValue:Boolean=false);
begin
if PropInfo <> nil then
begin
begin
rStrList.Add (PropInfo^.PropType^.Name );
if NeedValue then
rStrList.Strings [rStrList.Count -1]:=rStrList.Strings [rStrList.Count -1]+
'Value ='+ GetStrProp(Instance, PropInfo);
end;
end;
end;
end.

 
Help Help !
请高手Help !
 
接受答案了.
 
后退
顶部