Delphi: Changing Button Color

Question

How can I change the color of a button in Delphi?

Answer

None of the 3 kinds of Delphi buttons (TButton, TBitBtn, TSpeedButton) has a property "color". Here are some alternatives:

1. Use a TPanel as a TSpeedButton. It has an OnClick event, just like a button, but you also can set its color.

Additional advantages:
- You can play with the panel's BevelWidth.
- You can add a text label on the panel.
- To mimic a BitButton, add image on the panel.

To mimic the up/down behaviour of a button, write this code for the panel's OnMouseDown and OnMouseUp events:

procedure TForm1.Panel1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Panel1.BevelOuter := bvLowered;
end;

procedure TForm1.Panel1MouseUp(Sender: TObject; 
  Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); begin Panel1.BevelOuter := bvRaised; end;

2. Or download an existing custom color-button, there are some on several component sites (search for TColorButton or "colored button").

3. Or program your own color-button component. This is not an easy task, but there are examples on the Web.



Crash Course Delphi   Database Tutorial   FAQ   Source Code   Tips