gpt4 book ai didi

Delphi - 使用 GetPropValue() 获取属性值

转载 作者:行者123 更新时间:2023-12-03 15:19:05 34 4
gpt4 key购买 nike

我正在使用 Delphi 的 GetPropValue() 函数来获取 TControl 类型的某些对象的某些属性的值。当我获得简单的属性值(例如 ValueOpacity 等)时,一切正常,但当我使用 firemonkey 时,有一些扩展属性,例如 RotationCenter,它有RotationCenter.XRotationCenter.Y,甚至是TextSettings中的文本属性,在这些属性中子类型我无法获取值。

在这个例子中我正确地得到了值:

If IsPublishedProp (Component_cc, 'Value') then
EditValue.Text: = GetPropValue (Component_cc, 'Value', true);

其中Component_cc:TControl;并且是动态创建的,它也可以是任何类型的 Firemonkey 组件(到目前为止一切正常,一切正常)。

当我需要使用下面的表单时,它不起作用。

If IsPublishedProp (Component_cc, 'RotationCenter.X') then
EditRotationCenterX.Text: = GetPropValue (CC_component, 'RotationCenter.X', true);

有人知道如何通过此函数扩展这些属性吗?

最佳答案

首先,CC_component 的 RotationCenter 属性实际上是 TPosition 类的实例,该类派生自 TPercient

其次,调用 IsPublishedProp 时不能使用点符号。

您可以使用 GetObjectProp 首先检索内部 TPosition 实例,然后从那里访问 X 属性:

(假设一个简单的 FMX 应用程序,其一个表单包含一个名为 Button1TButton 和一个名为 EditRotationCenterXTEdit >.)

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 当前值中元素的文本表示形式。例如,如果 Set 的值为 [TCorner.BottonLeft, TCorner.TopRight],您将得到字符串值“TopRight,BottonLeft”。然后,您检查目标元素的名称是否出现在返回字符串中的任何位置。如果将来 Delphi RTL 或 FMX 库发生更改,此方法很容易失败。

(此示例向简单的 FMX 应用程序添加了一个名为 Rectangle1TRectangle 形状和一个名为 cbCornerBottonRightTCheckBox从上面:)

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;

关于Delphi - 使用 GetPropValue() 获取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45786088/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com