2017-08-31 57 views
5

我使用Delphi的GetObjectProp函数来获取表单组件的属性,我得到几个组件的所有属性与GetObjectProp TextSettings.Font.Style属性,但我不能让TextSettings.Font例如,像TLabel这样的组件的样式(粗体,斜体,...)属性。我需要知道组件文本是粗体还是斜体。我正在尝试获取这些属性的过程如下:获取采用Delphi东京10.2

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操作并不适用于这一运算对象类型

我在做什么错?

+0

在这个例子中我简化了代码更好理解,但在实际应用中它更复杂,在运行时创建组件,并且可以是任何Class,所以我使用rtti。我将其更改为TFontStyles segestao,但错误仍然存​​在。 – Anderson

+0

'Style'是'TFontStyles'类型,它不是一个对象类型,而是一组属性类型。而'fsBold'不是一个属性,而是该集合中可能的成员。 – Victoria

+0

但是,如果属性不是对象类型,我该如何获取属性? – Anderson

回答

6

GetObjectProp(Fonte, 'Style')将不起作用,因为Style不是基于对象的属性开始,它是基于Set的属性。而GetPropValue(Estilo, 'fsBold', true)是完全错误的(不是说你会得到远远不够无论如何称呼它),因为fsBold不是属性,它是TFontStyle枚举的成员。为了检索Style属性值,您将不得不使用GetOrdProp(Fonte, 'Style'),GetSetProp(Fonte, 'Style')GetPropValue(Fonte, 'Style')来代替(分别为integer,stringvariant)。

这就是说,一旦您检索到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; 

读英巴卡迪诺的文档了解更多信息:

Setting Text Parameters in FireMonkey

+0

谢谢,我将在码头上看到文档。您提供显示错误[dcc64错误] uPrincipal.pas示例代码(1343):E2003未声明的标识符: 'fsBold' – Anderson

+0

@Juliano:那是因为FMX使用[作用域枚举](http://docwiki.embarcadero.com/RADStudio/EN/Simple_Types_(DELPHI)#Scoped_Enumerations)。使用'TFontStyle.fsBold'代替 –

+0

的作品!感谢你们.. – Anderson