Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiFiremonkeyIOSOSXWindows

Build A Custom Button Using TRectangle In Delphi XE6 Firemonkey On Android And IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE6 Firemonkey Rectangle Button | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSDeveloper 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.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

Leave a Reply