加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用Delphi Tokyo 10.2使用GetObjectProp获取TextSettings.Font.

发布时间:2020-12-15 09:30:47 所属栏目:大数据 来源:网络整理
导读:我正在使用Delphi的GetObjectProp函数来获取表单组件的属性,我获得了几个组件的所有属性,但我无法获得像TLabel这样的组件的TextSettings.Font.Style(Bold,Italic,…)属性例如.我需要知道组件文本是粗体还是斜体.我正在尝试获取这些属性的过程如下: procedur
我正在使用Delphi的GetObjectProp函数来获取表单组件的属性,我获得了几个组件的所有属性,但我无法获得像TLabel这样的组件的TextSettings.Font.Style(Bold,Italic,…)属性例如.我需要知道组件文本是粗体还是斜体.我正在尝试获取这些属性的过程如下:

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 Error] uPrincipal.pas(1350): E2015 Operator not applicable to this operand type

我究竟做错了什么?

解决方法

GetObjectProp(Fonte,’Style’)将无法工作,因为Style不是基于对象的属性,它是一个基于Set的属性.并且GetPropValue(Estilo,’fsBold’,true)是完全错误的(并不是说你无论如何都会调到它),因为fsBold不是属性,它是TFontStyle枚举的成员.要检索Style属性值,您必须使用GetOrdProp(Fonte,’Style’),GetSetProp(Fonte,’Style’)或GetPropValue(Fonte,’Style’)(作为整数,字符串或变体,分别).

话虽如此,一旦您检索到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的文档了解更多详情:

Setting Text Parameters in FireMonkey

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读