Get TextSettings.Font.Style property with GetObjectProp using Delphi Tokyo 10.2

五迷三道 提交于 2019-12-23 10:09:40

问题


I'm using Delphi's GetObjectProp function to get the properties of the form components, I get all the properties of several components, but I can not get the TextSettings.Font.Style (Bold, Italic, ...) property of components like TLabel for example. I need to know if the component text is bold or italic. The procedure I am working on trying to get these properties follows below:

procedure Tfrm1.aoClicarComponente(Sender: TObject);
var
  TextSettings: TTextSettings;
  Fonte: TFont;
  Estilo: TFontStyle;
  Componente_cc: TControl; 
begin
Componente_cc := TControl(Label1);
if IsPublishedProp(Componente_cc, 'TextSettings') then
    begin
      TextSettings := GetObjectProp(Componente_cc, 'TextSettings') as TTextSettings;
        if Assigned(TextSettings) then
           Fonte := GetObjectProp(TextSettings, 'Font') as TFont;
        if Assigned(Fonte) then
           Estilo := GetObjectProp(Fonte, 'Style') as TFontStyle; // <-- error in this line 
        if Assigned(Estilo) then
           Edit1.text := GetPropValue(Estilo, 'fsBold', true);
    end
end; 

The error displayed on the line where I marked above is.

[dcc64 Error] uPrincipal.pas(1350): E2015 Operator not applicable to this operand type

What am I doing wrong?


回答1:


GetObjectProp(Fonte, 'Style') will not work since Style is not an object-based property to begin with, it is a Set-based property. And GetPropValue(Estilo, 'fsBold', true) is just plain wrong (not that you would get far enough to call it anyway), because fsBold is not a property, it is a member of the TFontStyle enum. To retreive the Style property value, you would have to use GetOrdProp(Fonte, 'Style'), GetSetProp(Fonte, 'Style'), or GetPropValue(Fonte, 'Style') instead (as an integer, string, or variant, respectively).

That being said, once you have retrieved the TextSettings object, you don't need to use RTTI at all to access its Font.Style property, just access the property directly.

Try this instead:

procedure Tfrm1.aoClicarComponente(Sender: TObject);
var
  Componente_cc: TControl;
  TextSettings: TTextSettings;
begin
  Componente_cc := ...;
  if IsPublishedProp(Componente_cc, 'TextSettings') then
  begin
    TextSettings := GetObjectProp(Componente_cc, 'TextSettings') as TTextSettings;
    Edit1.Text := BoolToStr(TFontStyle.fsBold in TextSettings.Font.Style, true);
  end;
end; 

A better (and preferred) solution is to not use RTTI at all. FMX classes that have a TextSettings property also implement the ITextSettings interface for exactly this situation, eg:

procedure Tfrm1.aoClicarComponente(Sender: TObject);
var
  Componente_cc: TControl;
  Settings: ITextSettings;
begin
  Componente_cc := ...;
  if Supports(Componente_cc, ITextSettings, Settings) then
  begin
    Edit1.Text := BoolToStr(TFontStyle.fsBold in Settings.TextSettings.Font.Style, true);
  end;
end; 

Read Embarcadero's documentation for more details:

Setting Text Parameters in FireMonkey



来源:https://stackoverflow.com/questions/45972320/get-textsettings-font-style-property-with-getobjectprop-using-delphi-tokyo-10-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!