本文介绍了使用Delphi Tokyo 10.2通过GetObjectProp获取TextSettings.Font.Style属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi的GetObjectProp函数获取表单组件的属性,获得了多个组件的所有属性,但无法获得以下组件的TextSettings.Font.Style(粗体,斜体,...)属性像TLabel这样的组件。我需要知道组件文本是粗体还是斜体。我正在尝试获取这些属性的过程如下:

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.

我在做什么错了?

推荐答案

GetObjectProp(Fonte,'Style') 不起作用,因为 Style 并不是以对象为基础的属性,而是 Set 的属性。而 GetPropValue(Estilo,'fsBold',true)完全是错误的(并不是您会得到足够的称呼),因为 fsBold 不是属性,它是 TFontStyle 枚举的成员。要获取 Style 属性值,您将必须使用 GetOrdProp(Fonte,'Style') GetSetProp(Fonte,'Style') GetPropValue(Fonte,'Style')代替(作为整数字符串变体)。

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).

也就是说,一旦检索到 TextSettings 对象,就完全不需要使用RTTI来访问其 Font.Style 属性,只需直接访问该属性即可。

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.

尝试以下操作:

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类也实现了接口,例如:

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;

有关详细信息,请阅读Embarcadero的文档:

Read Embarcadero's documentation for more details:

这篇关于使用Delphi Tokyo 10.2通过GetObjectProp获取TextSettings.Font.Style属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:56