- 註冊時間
- 2006-3-13
- 最後登錄
- 2025-1-10
- 在線時間
- 673 小時
- 閱讀權限
- 200
- 積分
- 417
- 帖子
- 1107
- 精華
- 0
- UID
- 2
  
|
通過 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;
複製代碼 |
|