首先,CC_component 的RotationCenter 属性实际上是TPosition 类的一个实例,它继承自TPersistent。
其次,调用IsPublishedProp时不能使用点分符号。
您可以使用GetObjectProp 首先检索内部TPosition 实例,然后从那里访问X 属性:
(假设一个简单的 FMX 应用程序具有一个表单,其中包含一个名为 Button1 的 TButton 和一个名为 EditRotationCenterX 的 TEdit。)
procedure TForm1.Button1Click(Sender: TObject);
var
CC_component : TComponent;
CC_component_RotationCenter : TPosition;
begin
CC_component := Button1;
if IsPublishedProp(CC_component, 'RotationCenter') then
begin
CC_component_RotationCenter := TPosition(GetObjectProp(CC_component, 'RotationCenter'));
EditRotationCenterX.Text := CC_component_RotationCenter.X.ToString;
end
end;
更新,对于 Set 类型的属性:
对于 Set 类型的属性,您需要使用 GetOrdProp 检索其序数值。这将是表示当前值中包含哪些元素的位数组。然后,您只需测试是否设置了适当的位。这是我更喜欢的方法。
或者,您可以使用GetSetProp,它将返回集合当前值中元素的文本表示。例如,如果 Set 的值为[TCorner.BottonLeft, TCorner.TopRight],您将返回字符串值“TopRight,BottonLeft”。然后检查目标元素的名称是否出现在返回的字符串中的任何位置。如果将来更改 Delphi RTL 或 FMX 库,此方法很容易失败。
(此示例将名为 Rectangle1 的 TRectangle 形状和名为 cbCornerBottonRight 的 TCheckBox 添加到上面的简单 FMX 应用程序中:)
procedure TForm1.Button1Click(Sender: TObject);
var
CC_component : TComponent;
CC_component_Corners : nativeint;
CC_component_CornersAsString : string;
begin
CC_component := Rectangle1;
if IsPublishedProp(CC_component, 'Corners') then
begin
// Using this method will make your code less sensitive to
// changes in the ordinal values of the Set's members or
// changes to names of the enumeration elements.
//
CC_component_Corners := GetOrdProp(CC_component,'Corners');
cbCornerBottonRight.IsChecked := ((1 shl ord(TCorner.BottomRight)) and CC_component_Corners) <> 0;
// This approach may break if the names of the elements of
// the TCorner enumeration are ever changed. (BTW, they have
// been in the past: "cvTopLeft", "cvTopRight", "cvBottomLeft",
// and "cvBottomRight" are now deprecated)
//
CC_component_CornersAsString := GetSetProp(CC_component,'Corners');
cbCornerBottonRight.IsChecked := CC_component_CornersAsString.IndexOf('BottomRight') >= 0;
end;
end;