【发布时间】:2011-11-08 16:13:54
【问题描述】:
我只是想在按下按钮后更改它的颜色。
我必须使用“样式”来执行此操作还是....?
【问题讨论】:
-
澄清:您是否希望按钮在按下时更改颜色,然后恢复,或者在按下后永久更改颜色(如“向下”的速度按钮) - 如果是这样,当它会重置为原始颜色吗?
标签: delphi firemonkey
我只是想在按下按钮后更改它的颜色。
我必须使用“样式”来执行此操作还是....?
【问题讨论】:
标签: delphi firemonkey
您可以更改 button.StyleLookup 属性以更改样式(颜色)。
您需要向样式簿添加新样式。
【讨论】:
TRectangle,只有 TStyleContainer、TButtonStyleObject、TGlyph、TButtonStyleTextObject 和 TImage .
使用样式
创建不同样式并切换到新样式的另一种方法是为按钮创建自定义样式并在运行时更改该样式的颜色。
您刚刚为按钮创建了自定义样式。所以当你在运行时编辑它时,它只会影响那个按钮。
现在,在您的 OnClick 事件中输入以下内容以在运行时更改颜色:
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;
注意:如果您还没有 FMX.Objects 子句,请将其添加到您的 uses 子句中。这就是 TRectangle 所在的位置。
但是等等……
您会注意到,当鼠标离开或进入按钮时,按钮的颜色会变回默认值。那是因为动画。如果您在自定义样式的样式编辑器中为两个 TColorAnimation 样式元素设置 stylename 属性,您还可以在它们上设置颜色。对于我的示例,我将 TColorAnimations 命名为 coloranimation1 和 coloranimation2。
这是修改后的代码:
var
r: TRectangle;
ca: TColorAnimation;
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;
ca := (Button1.FindStyleResource('coloranimation1') as TColorAnimation);
if Assigned(ca) then
begin
ca.StartValue := claBlue;
end;
ca := (Button1.FindStyleResource('coloranimation2') as TColorAnimation);
if Assigned(ca) then
begin
ca.StopValue := claBlue;
end;
注意:将 FMX.Ani 添加到 TColorAnimation 的 uses 子句中。
【讨论】: