FMXGradient (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the usage of several properties of TGradient.

To replicate this example, create a new multi-device application and place the following components on your form:

The form should look like in the following image.

TGradient0.JPG

Code

//C++
// ---------------------------------------------------------------------------

void __fastcall TForm1::ColorComboBox1Change(TObject *Sender) {
	if (Ellipse1->Visible) {
		Ellipse1->Fill->Gradient->Color = ColorComboBox1->Color;
	}
	else {
		Rectangle1->Fill->Gradient->Color = ColorComboBox1->Color;
	}
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::ColorComboBox2Change(TObject *Sender) {
	if (Ellipse1->Visible) {
		Ellipse1->Fill->Gradient->Color1 = ColorComboBox2->Color;
	}
	else {
		Rectangle1->Fill->Gradient->Color1 = ColorComboBox2->Color;
	}
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Edit1Change(TObject *Sender) {
	if (Ellipse1->Visible) {
		Ellipse1->Fill->Gradient->RadialTransform->RotationAngle =
			StrToFloat(Edit1->Text);
	}
	else {
		Rectangle1->Fill->Gradient->StartPosition->X = StrToFloat(Edit1->Text);
	}
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Edit2Change(TObject *Sender) {
	if (Ellipse1->Visible) {
		Ellipse1->Fill->Gradient->RadialTransform->RotationCenter->X =
			StrToFloat(Edit2->Text);
	}
	else
		Rectangle1->Fill->Gradient->StartPosition->Y = StrToFloat(Edit2->Text);
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Edit3Change(TObject *Sender) {
	if (Ellipse1->Visible) {
		Ellipse1->Fill->Gradient->RadialTransform->RotationCenter->Y =
			StrToFloat(Edit3->Text);
	}
	else {
		Rectangle1->Fill->Gradient->StopPosition->X = StrToFloat(Edit3->Text);
	}
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Edit4Change(TObject *Sender) {
	Rectangle1->Fill->Gradient->StopPosition->Y = StrToFloat(Edit4->Text);
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender) {
	Ellipse1->Visible = False;
	Rectangle1->Visible = False;

	// Fill initialization so Gradient can be applied
	Ellipse1->Fill->Kind = TBrushKind::Gradient;
	Rectangle1->Fill->Kind = TBrushKind::Gradient;

        Ellipse1->Fill->Gradient->Style = TGradientStyle::Radial;
	Rectangle1->Fill->Gradient->Style = TGradientStyle::Linear;
	Label1->Text = "Choose the gradient type";
	ListBoxItem1->Text = "Linear";
	ListBoxItem2->Text = "Radial";
	Label2->Text = "";
	Label3->Text = "";
	Label4->Text = "";
	Label5->Text = "";
	Edit1->Visible = False;
	Edit2->Visible = False;
	Edit3->Visible = False;
	Edit4->Visible = False;
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::ListBoxItem1Click(TObject *Sender) {
	Label2->Text = "StartPosition->X: ";
	Label3->Text = "StartPosition->Y: ";
	Label4->Text = "StopPosition->X: ";
	Label5->Text = "StopPosition->Y: ";
	Edit1->Visible = True;
	Edit2->Visible = True;
	Edit3->Visible = True;
	Edit4->Visible = True;
	Ellipse1->Visible = False;
	Rectangle1->Visible = True;
	// Set the Gradient colors
	Rectangle1->Fill->Gradient->Color1 = ColorComboBox2->Color;
	Rectangle1->Fill->Gradient->Color = ColorComboBox1->Color;
	// Populate the edit boxes
	Edit1->Text = FloatToStr(Rectangle1->Fill->Gradient->StartPosition->X);
	Edit2->Text = FloatToStr(Rectangle1->Fill->Gradient->StartPosition->Y);
	Edit3->Text = FloatToStr(Rectangle1->Fill->Gradient->StopPosition->X);
	Edit4->Text = FloatToStr(Rectangle1->Fill->Gradient->StopPosition->Y);
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::ListBoxItem2Click(TObject *Sender) {
	Label2->Text = "RotationAngle:";
	Label3->Text = "RotationCenter->X:";
	Label4->Text = "RotationCenter->Y:";
	Label5->Text = "";
	Edit1->Visible = True;
	Edit2->Visible = True;
	Edit3->Visible = True;
	Edit4->Visible = False;
	Ellipse1->Visible = True;
	Rectangle1->Visible = False;
	// Set the Gradient colors
	Ellipse1->Fill->Gradient->Color1 = ColorComboBox2->Color;
	Ellipse1->Fill->Gradient->Color = ColorComboBox1->Color;

	// Populate the edit boxes
	Edit1->Text =
		FloatToStr(Ellipse1->Fill->Gradient->RadialTransform->RotationAngle);
	Edit2->Text =
		FloatToStr(Ellipse1->Fill->Gradient->RadialTransform->
		RotationCenter->X);
	Edit3->Text =
		FloatToStr(Ellipse1->Fill->Gradient->RadialTransform->
		RotationCenter->Y);
}

The result is an application that uses the properties of TGradient to paint a TRectangle or a TEllipse. The user can select the gradient type and show the appropriate object (Rectangle for the Linear style and Ellipse for the Radial style). The user can select the colors of the gradient, using the two TColorComboBox objects. According to the gradient type, the user can either select the starting and stopping position (for Linear), or the rotation angle and rotation center (for Radial).

The Rectangle is enabled: TGradient1.JPG
The start position is changed: TGradient3.JPG
The stop position is changed: TGradient4.JPG
The Ellipse is enabled: TGradient5.JPG
The rotation center is changed: TGradient7.JPG

Uses


See also