Delphi - Retrieving Property Values ​​Using GetPropValue ()

I use the Delphi function GetPropValue()to get the values ​​of some properties of some type objects TControl. Everything works correctly when I get simple property values, such as Value, Opacityetc., but since I use firemonkey, there are some advanced properties, such as RotationCenterit has RotationCenter.Xand RotationCenter.Yor even text properties inside TextSettings, in these properties with subtypes I don’t I can get the values.

In this example, I get the values ​​correctly:

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

Where Component_cc:TControl; And it is created dynamically, it can be any Firemonkey component (as long as everything is in order, everything works).

When I need to use the form below, it does not work.

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

Does anyone know a way to extend these properties with this function?

+4
source share
2 answers

First, the CC_component property RotationCenteris actually an instance of a class TPositionthat deviates from TPersistent.

Secondly, you cannot use dotted notation when calling IsPublishedProp.

You can use GetObjectPropto first extract the internal instance TPosition, and then access the property X:

(Suppose a simple FMX application has one form containing TButton, called, Button1and a TEdit, called EditRotationCenterX.)

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;

Update for a property of type Set:

Set type GetOrdProp. , , . , . , .

GetSetProp, Set Set. , Set [TCorner.BottonLeft, TCorner.TopRight], "TopRight, BottonLeft". , . , Delphi RTL FMX - .

( TRectangle, Rectangle1 TCheckBox, cbCornerBottonRight, 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 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;
+6

RTTI, . . X TPosition:

var
  O: TObject;
  X: Integer;
begin
  if PropIsType(Component_cc, 'RotationCenter', tkClass) then
  begin
    O := GetObjectProp(Component_cc, 'RotationCenter');
    if Assigned(O) and PropIsType(O, 'X', tkInteger) then
      X := GetOrdProp(O, 'X');
  end;
end;
+5

Source: https://habr.com/ru/post/1684069/


All Articles