2

我的应用程序有几个TSpeedButtons可供选择的颜色,我希望每个选择都以TSpeedButton.

我在Stackoverflow上找到了关于如何更改TButton. 第二个答案(动态更改颜色)似乎是我正在寻找的解决方案。内容如下:

var r: TRectangle;
begin
// Find the background TRectangle style element for the button
   r := (Button1.FindStyleResource('background') as TRectangle);
   if Assigned(r) then
   begin
      r.Fill.Color := claBlue;
   end;
end;

这不再起作用(我使用 XE5,这是 XE2?)。它在r := ...语句中生成一个异常:

"illegal cast". 

FindStyleResource 返回一个 FMXObject。

TRectangle 是一个 TShape->TControl->TFMXObject。

我可以转换为 TControl 但不能转换为 TShape。如果您想知道,Button1 是一个 TButton。

有谁知道我如何改变 a 的颜色TSpeedButton

顺便说一句:有没有办法确定究竟返回了哪种类型的对象?我在调试器中找不到。

4

2 回答 2

3

The answer to the question you linked to relates to vector styles, where the style constructed entirely from shapes etc (such as the TRectangle).

In newer versions of FireMonkey the 'system' styles (which mimic the OS look) and some other styles use bitmaps.

If you want to edit a bitmap style, you'll need to find the bitmap image in the style, edit it, and then redo/edit the button's style to use the new image. (If you're on mobile this will probably be hard enough that you shouldn't even try it).

Another route would be be to change to one of the bitmap styles supplied with Delphi. You will find them under the redist/styles/fmx folder of your Delphi installation.

As for the class of the object, and as per other comments, examine the ClassName property of the object returned.

But bear in mind that not every style will have an object called 'background'. Both the name of the object and it's class can easily vary between styles. You really ought to look at the style you want to pluck objects from to see what's there. (Note that the objects name ('background') will be in the StyleName property).

于 2013-10-25T19:16:58.870 回答
1

改用 a 会容易得多TColorButton,它直接公开Color属性。您可以Colors在组件面板的页面上找到它。以下是 IDE 表单设计器中新 FMX 表单中的两个:

两个 TColorButtons 的图像

至于“正在返回哪种类型的对象”,您可以使用调试器或 aShowMessage作为TFMXObject.ClassName返回值:

var 
  Obj: TFmxObject; 
begin 
  Obj := Button1.FindResource('background'); 
  if Assigned(Obj) then 
    ShowMessage(Obj.ClassName); 
end; 
于 2013-10-25T23:49:09.630 回答