使用Delphi Tokyo 10.2获取带有GetObjectProp的TextSettings.font.Style



我正在使用delphi的getObjectProp函数来获取表单组件的属性,我获得了几个组件的所有属性,但是我无法获得textsettings.font.font.style(bold,bold,斜体,...)例如Tlabel之类的组件的属性。我需要知道组件文本是大胆还是斜体。我正在尝试尝试获取这些属性的过程如下:

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; 

我上面标记的线路上显示的错误是

[DCC64错误] Uprincipal.pas(1350):E2015运算符不适用于此操作数类型

我在做什么错?

GetObjectProp(Fonte, 'Style')无法使用,因为Style不是基于对象的属性,而是基于Set的属性。GetPropValue(Estilo, 'fsBold', true)只是完全错误的(并不是说您会远远足以称呼它),因为fsBold不是属性,而是TFontStyle枚举的成员。要重述Style属性值,您必须改用GetOrdProp(Fonte, 'Style')GetSetProp(Fonte, 'Style')GetPropValue(Fonte, 'Style')(分别为integerstringvariant)。

说,一旦您检索了TextSettings对象,就无需使用RTTI访问其Font.Style属性,只需直接访问该属性。

而是尝试一下:

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; 

更好(和首选)解决方案是完全不使用RTTI。具有TextSettings属性的FMX类还针对这种情况实现了ITextSettings接口,例如:

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; 

阅读Embarcadero的文档以获取更多详细信息:

设置FiremonKey中的文本参数

最新更新