首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Delphi XE2 FireMonkey中-如何在按下按钮后更改它的颜色

在Delphi XE2 FireMonkey中-如何在按下按钮后更改它的颜色
EN

Stack Overflow用户
提问于 2011-11-09 00:13:55
回答 2查看 16.8K关注 0票数 7

我只想在按下按钮后改变它的颜色。

我是不是必须使用“样式”来完成这项工作,或者...?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-09 02:21:53

您可以更改button.StyleLookup属性以更改样式(颜色)。

您需要将新样式添加到Stylebook中。

  1. 选择‘编辑自定义样式...’单击鼠标右键菜单中的一个按钮。当您没有更改其Button1Style1

的名称时,将background:TRectangle

  • Apply下的TRectangle项的Fill.Color属性更改并在您的按钮单击中关闭Stylebook

  • Clear button.stylelookup

  • Change

  • Fill.Color为新的create style
票数 8
EN

Stack Overflow用户

发布于 2011-11-09 04:02:08

使用样式的

创建不同样式并切换到该新样式的另一种方法是为按钮创建自定义样式,然后在运行时更改该样式中的颜色。

  1. 右键单击该按钮,然后选择“编辑自定义样式...”在主菜单中,单击样式编辑器中的[应用并关闭]。

您刚刚为该按钮创建了一个自定义样式。因此,当您在运行时编辑它时,它只会影响该按钮。

现在,在OnClick事件中输入以下内容以在运行时更改颜色:

代码语言:javascript
运行
复制
  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。

以下是修改后的代码:

代码语言:javascript
运行
复制
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子句中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8053545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档