FMXTCanvasFillFunctions (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the TCanvas fill functions and their results.

The TCanvas fill functions are: Fill, FillArc, FillEllipse, FillPath, FillPolygon, FillRect, FillText.

To build and test this example, create a Multi-Device Application - C++ and add the following controls on the form:

The example draws on the canvas of the bitmap. The bitmap is displayed on the TImage.

Code

Add the following code to the OnCreate event handler of the form.

void __fastcall TForm2::FormCreate(TObject *Sender) {
	Image1->Bitmap->Clear(0xFFFFFF);
	// initializes the image bitmap canvas 
	Image1->Bitmap = new TBitmap(400, 400);
}

Add the following code, which makes the drawing area white, to the OnClick event handler of the button.

void __fastcall TForm2::Button1Click(TObject *Sender) {
	// clears the canvas
	Image1->Bitmap->Canvas->Clear(0xFFFFFF);
}

Add the following code, which changes the fill color of the canvas, to the OnExit event handler of the ColorComboBox.

void __fastcall TForm2::ColorComboBox1Exit(TObject *Sender) {
	// sets the fill color of the canvas to the value from the ColorComboBox 
	Image1->Bitmap->Canvas->Fill->Color = ColorComboBox1->Color;
}

Add the following codes to the OnClick event handlers of each radio button.

Code

void __fastcall TForm2::RadioButton11Click(TObject *Sender) {
	Image1->Bitmap->Clear(0xFFFFFF);
	// sets the center and the radius of the arc
	TPointF p1(200, 200), p2(150, 150);
	Image1->Bitmap->Canvas->BeginScene();
	// fills and draws the arc on the canvas
	Image1->Bitmap->Canvas->FillArc(p1, p2, 90, 230, 20);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::RadioButton12Click(TObject *Sender) {
	Image1->Bitmap->Clear(0xFFFFFF);
	// sets the circumscribed rectangle of the ellipse
	TRectF MyRect(50, 40, 200, 270);
	Image1->Bitmap->Canvas->BeginScene();
	// fills and draws the ellipse on the canvas
	Image1->Bitmap->Canvas->FillEllipse(MyRect, 40);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::RadioButton13Click(TObject *Sender) {
	TPathData* path;
	Image1->Bitmap->Clear(0xFFFFFF);
	// sets the circumscribed rectangle of the ellipse to be added to the path
	TRectF MyRect1(90, 100, 230, 300), MyRect2(70, 90, 220, 290);
	// initializes and creates the path to be drawn
	path = new TPathData;
	path->AddEllipse(MyRect1);
	path->AddRectangle(MyRect2, 0, 0, AllCorners);
	Image1->Bitmap->Canvas->BeginScene();
	// fills and draws the path on the canvas
	Image1->Bitmap->Canvas->FillPath(path, 200);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::RadioButton14Click(TObject *Sender) {
	Image1->Bitmap->Clear(0xFFFFFF);
	// sets the rectangle to be drawn
	TRectF MyRect(50, 40, 200, 270);
	Image1->Bitmap->Canvas->BeginScene();
	// fills and draws the rectangle on the canvas
	Image1->Bitmap->Canvas->FillRect(MyRect, 30, 60, AllCorners, 100);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::RadioButton15Click(TObject *Sender) {
	Image1->Bitmap->Clear(0xFFFFFF);
	// sets the points that define the polygon
	TPointF p1(210, 220), p2(330, 360), p3(380, 260), p4(200, 180),
		p5(140, 160);
	TPolygon MyPolygon;
	// creates the polygon
	MyPolygon.set_length(5);
	MyPolygon[0] = p1;
	MyPolygon[1] = p2;
	MyPolygon[2] = p3;
	MyPolygon[3] = p4;
	MyPolygon[4] = p5;
	Image1->Bitmap->Canvas->BeginScene();
	// fills and draws the polygon on the canvas
	Image1->Bitmap->Canvas->FillPolygon(MyPolygon, 50);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::RadioButton16Click(TObject *Sender) {
	Image1->Bitmap->Clear(0xFFFFFF);
	// sets the rectangle where the text will be displayed
	TRectF MyRect(50, 40, 200, 270);
	Image1->Bitmap->Canvas->BeginScene();
	// fills and draws the text in the specified rectangle area of the canvas
	Image1->Bitmap->Canvas->FillText(MyRect, "FillText", false, 100,
		TFillTextFlags(TFillTextFlag::RightToLeft), Center, Center);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

Uses

See Also

Personal tools
Assistance