7

ボタンを押した後にボタンの色を変更したいだけです。

これを行うには「スタイル」を使用する必要がありますか....?

4

2 に答える 2

8

button.StyleLookupプロパティを変更して、スタイル(色)を変更できます。

スタイルブックに新しいスタイルを追加する必要があります。

  1. ボタンからマウスの右ボタンメニューで[カスタムスタイルの編集...]を選択します。
  2. 背景の下のTRectangleアイテムからFill.Colorプロパティを変更します:TRectangle
  3. スタイルブックを適用して閉じる
  4. button.stylelookupをクリアします
  5. Button1Style1の名前を変更しなかった場合は、buttonclickのbutton.stylelookupを新しい作成スタイルに変更します。
于 2011-11-08T18:21:52.887 に答える
2

スタイルの使用

別のスタイルを作成してその新しいスタイルに切り替える代わりに、ボタンのカスタム スタイルを作成し、実行時にそのスタイルの色を変更します。

  1. ボタンを右クリックし、メイン メニューから [カスタム スタイルの編集...] を選択します。
  2. スタイル エディタで [適用して閉じる] をクリックします。

ボタンのカスタム スタイルを作成しました。したがって、実行時に編集すると、そのボタンにのみ影響します。

ここで、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;

注: TColorAnimation の uses 句に FMX.Ani を追加します。

于 2011-11-08T20:02:07.687 に答える