Allan Fernandes
Allan Fernandes

Reputation: 121

how to change, disabled button color FMX?

When using StyleBook, if button is Diasbled the color hardly describes that the button is disabled. I therefore want to change the color when the button is disabed. How do I do that. I am using Delphi Seattle

Upvotes: 3

Views: 1542

Answers (2)

Tupel
Tupel

Reputation: 318

A little late to the party, but I had the same issue. The solution I came up with was making my own descendant to TButton where I, just like @DNR, set the DisabledOpacity to 1.0. But rather than having a complete separate style for the disabled version of my button I implemented animation/effect triggers for setting the controls enabled property. This makes is possible to use color animations in your button style.

Some code snippets:

The interface

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

The implementation

{ 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;

Let's say you have a button with a TRectangle background, you can do this in the style:

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
...

Upvotes: 2

DNR
DNR

Reputation: 1659

That's not as straight-forward as changing the normal color in the Style Book. The disabled look is achieved by lowering the opacity of the control rather than by adjusting any of the color properties.

What I would do is to first create a style for the disabled button, using whichever colors you like. Whenever you disable the button, you can set the button's StyleLookup to that style's name, and change it back to the default if you enable it again.

In addition to that, you might want to disable the usual behavior of adjusting the opacity. Therefor you need to set the DisabledOpacityof the button. You can't usually access that property, but you can crack it open by subclassing it.

type
  TMyButton = class (TButton);

// ...

TMyButton(Button1).DisabledOpacity := 1.0;

Upvotes: 3

Related Questions