Developer Antonio Estevez has some sample source code for implementing a TRectangle component as a button in Delphi XE6 Firemonkey (and AppMethod). This technique should run cross platform on Windows, IOS, OSX, and Android. The code uses Mouse Enter, Mouse Leave, Mouse Down, and Mouse up events to change the color of the TRectangle and give it the look of being pressed. TRectangle is a really fast Firemonkey component and you can even apply a bitmap to it using it’s Fill.Bitmap property. Check out the code snippet below:
type
TForm2 = class(TForm)
RectButton: TRectangle;
Label1: TLabel;
procedure RectButtonMouseEnter(Sender: TObject);
procedure RectButtonMouseLeave(Sender: TObject);
procedure RectButtonMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure RectButtonClick(Sender: TObject);
procedure RectButtonMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
DefaultColor: TAlphaColor;
HiLightColor: TAlphaColor;
PressedColor: TAlphaColor;
procedure SetButtonColor(Sender: TObject; Color: TColor);
public
{ Public declarations }
end;
procedure TForm2.SetButtonColor(Sender: TObject; Color: TColor);
var
Button: TRectangle;
begin
if Sender is TRectangle then
begin
Button:= Sender as TRectangle;
Button.Fill.Color:= Color;
end;
end;
procedure TForm2.RectButtonClick(Sender: TObject);
begin
Label1.Text:= 'Button clicked';
end;
procedure TForm2.RectButtonMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
SetButtonColor(Sender, PressedColor);
end;
procedure TForm2.RectButtonMouseEnter(Sender: TObject);
begin
SetButtonColor(Sender, HiLightColor);
end;
procedure TForm2.RectButtonMouseLeave(Sender: TObject);
begin
SetButtonColor(Sender, DefaultColor);
end;
procedure TForm2.RectButtonMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
SetButtonColor(Sender, HiLightColor);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
DefaultColor:= $FF0000D0;
HiLightColor:= $FF0000FF;
PressedColor:= $FF0000B0;
SetButtonColor(RectButton, DefaultColor);
end;
Head over and check out the full thread on the Embarcadero forum where the code snippet is posted.
1 Comment