【问题标题】:how to change, disabled button color FMX?如何更改,禁用按钮颜色 FMX?
【发布时间】:2017-01-16 06:26:57
【问题描述】:

当使用样书时,如果按钮被禁用,颜色很难描述按钮被禁用。因此,我想在禁用按钮时更改颜色。我怎么做。 我正在使用 Delphi Seattle

【问题讨论】:

    标签: delphi firemonkey


    【解决方案1】:

    聚会有点晚了,但我遇到了同样的问题。我想出的解决方案是让我自己的后代到 TButton,就像@DNR 一样,我将 DisabledOpacity 设置为 1.0。但是,我没有为我的按钮的禁用版本设置一个完全独立的样式,而是实现了动画/效果触发器来设置控件的启用属性。这使得在您的按钮样式中使用彩色动画成为可能。

    一些代码sn-ps:

    界面

    TMyButton = class(TButton)
    protected
      procedure ApplyStyle; override;
      procedure ApplyTriggers; override;
      procedure SetEnabled(const Value: Boolean); override;
    public
      constructor Create(AOwner: TComponent); override;
    end;
    

    实现

    { TMyButton }
    
    procedure TMyButton.ApplyStyle;
    begin
      inherited;
      if not Enabled then
        ApplyTriggers;
    end;
    
    procedure TMyButton.ApplyTriggers;
    begin
      StartTriggerAnimation(Self, 'Enabled');
      ApplyTriggerEffect(Self, 'Enabled');
      inherited;
    end;
    
    constructor TMyButton.Create(AOwner: TComponent);
    begin
      inherited;
      DisabledOpacity := 1;
    end;
    
    procedure TMyButton.SetEnabled(const Value: Boolean);
    var
      LOldValue: Boolean;
    begin
      LOldValue := Enabled;
      inherited;
      if IsInflated and (LOldValue <> Value) then
        ApplyTriggers;
    end;
    

    假设您有一个带有 TRectangle 背景的按钮,您可以在样式中这样做:

    object TLayout
      StyleName = 'TMyButtonStyle'
      Align = Center
      Size.Width = 200.000000000000000000
      Size.Height = 50.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
      object TRectangle
        StyleName = 'background'
        Align = Contents
        Fill.Color = claWhite
        HitTest = False
        Size.Width = 200.000000000000000000
        Size.Height = 50.000000000000000000
        Size.PlatformDefault = False
        object TColorAnimation
          StyleName = 'caFillEnabled'
          Duration = 0.200000002980232200
          PropertyName = 'Fill.Color'
          StartValue = claWhite
          StopValue = claLightgray
          Trigger = 'Enabled=false'
          TriggerInverse = 'Enabled=true'
        end
    ...
    

    【讨论】:

      【解决方案2】:

      这不像在样式手册中更改正常颜色那么简单。禁用外观是通过降低控件的不透明度而不是通过调整任何颜色属性来实现的。

      我要做的是首先为禁用的按钮创建一个样式,使用您喜欢的任何颜色。每当您禁用按钮时,您都可以将按钮的StyleLookup 设置为该样式的名称,如果再次启用,则将其更改回默认值。

      除此之外,您可能希望禁用调整不透明度的常规行为。因此您需要设置按钮的DisabledOpacity。您通常无法访问该属性,但您可以通过继承它来破解它。

      type
        TMyButton = class (TButton);
      
      // ...
      
      TMyButton(Button1).DisabledOpacity := 1.0;
      

      【讨论】:

      • DisabledOpacity 最佳选择
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多