bestlong 怕失憶論壇
標題:
根據 RTTI 給子對象的屬性賦值
[打印本頁]
作者:
bestlong
時間:
2010-8-9 13:45
標題:
根據 RTTI 給子對象的屬性賦值
通過 RTTI 給對象的屬性賦值相信大家都知道, 相應資料比較多, 但如何用RTTI為含有子對象(如FONT)的屬性賦值?
屬性必須是 published 的, 也就是可以在運行期在 Object Inspector 上設置的屬性
//uses typinfo
//取得對象屬性值,如果存在
function GetObjectProperty(const AObject : TObject; const APropName : string):TObject;
var
PropInfo: PPropInfo;
begin
Result := nil;
PropInfo := GetPropInfo(AObject.ClassInfo, APropName);
if Assigned(PropInfo) and (PropInfo^.PropType^.Kind = tkClass) then
Result := GetObjectProp(AObject, PropInfo);
end;
//給整數型態屬性賦值,如果存在
function SetIntegerPropertyIfExists(const AObject : TObject; const APropName : string; const AValue : integer):Boolean;
var
PropInfo:PPropInfo;
begin
PropInfo := GetPropInfo(AObject.ClassInfo, APropName);
if Assigned(PropInfo) and (PropInfo^.PropType^.Kind = tkInteger) then
begin
SetOrdProp(AObject, PropInfo, AValue);
Result := True;
end else
Result := False;
end;
//調用,把窗體的 Font.Size 屬性設為 9
procedure TFrmTest.FormCreate(Sender: TObject);
var
objTemp : TObject;
begin
objTemp := GetObjectProperty(Self, 'Font');
if Assigned(objTemp) then
SetIntegerPropertyIfExists(objTemp, 'Size', 9);
end;
複製代碼
歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/)
Powered by Discuz! X1.5