FMXTCanvasDrawFunctions (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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

The TCanvas drawing functions are: DrawArc, DrawBitmap, DrawEllipse, DrawLine, DrawPath, DrawPolygon, DrawRect, DrawRectSides, CreateThumbnail.

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 TForm1::FormCreate(TObject *Sender) {
	// sets the size of the TBitmap  
	Image1->Bitmap->SetSize(int(Image1->Width),int(Image1->Height));
	Image1->Bitmap->Clear(TAlphaColorRec::White);
}

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->Clear(TAlphaColorRec::White);
}


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

Code

void __fastcall TForm1::DrawArcClickClick(TObject *Sender) {
	// sets the center and the radius of the arc
	TPointF p1(200, 200), p2(150, 150);
	Image1->Bitmap->Canvas->BeginScene();
	// draws the arc on the canvas
	Image1->Bitmap->Canvas->DrawArc(p1, p2, 90, 230, 20);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawBitmapClick(TObject *Sender) {
	TBitmap* MyBitmap;
	// is the area where to draw the bitmap
	// is also the area of the bitmap to be drawn on the canvas 
	TRectF MyRect(50, 30, 150, 200);
	// MyBitmap is the bitmap to be drawn on the canvas
	MyBitmap = new TBitmap("bitmap1.png");
	Image1->Bitmap->Canvas->BeginScene();
	// draws on the rectangle specified by MyRect the area from MyBitmap specified by MyRect
	Image1->Bitmap->Canvas->DrawBitmap(MyBitmap, MyRect, MyRect, 20);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawEllipseClick(TObject *Sender) {
	// sets the circumscribed rectangle of the ellipse
	TRectF MyRect(50, 40, 200, 270);
	Image1->Bitmap->Canvas->BeginScene();
	// draws the ellipse on the canvas
	Image1->Bitmap->Canvas->DrawEllipse(MyRect, 40);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawLineClick(TObject *Sender) {
	// p1 and p2 represent the ends of the line to be drawn
	TPointF p1(20, 2), p2(350, 400);
	Image1->Bitmap->Canvas->BeginScene();
	// draws the line on the canvas
	Image1->Bitmap->Canvas->DrawLine(p1, p2, 100);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawPathClick(TObject *Sender) {
	TPathData* path;
	// 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);
	// draws the path on the canvas
	Image1->Bitmap->Canvas->BeginScene();
	Image1->Bitmap->Canvas->DrawPath(path, 200);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawPolygonClick(TObject *Sender) {
	// sets the points that define the polygon
	TPointF p1(80, 200), p2(225, 30), p3(370, 200), p4(300, 340),
		p5(150, 340), p6(80,200);
	TPolygon MyPolygon;
	// creates the polygon
	MyPolygon.set_length(6);
	MyPolygon[0] = p1;
	MyPolygon[1] = p2;
	MyPolygon[2] = p3;
	MyPolygon[3] = p4;
	MyPolygon[4] = p5;
        MyPolygon[5] = p6;
	Image1->Bitmap->Canvas->BeginScene();
	// draws the polygon on the canvas
	Image1->Bitmap->Canvas->DrawPolygon(MyPolygon, 50);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawRectClick(TObject *Sender) {
	// sets the rectangle to be drawn
	TRectF MyRect(50, 40, 200, 270);
	Image1->Bitmap->Canvas->BeginScene();
	// draws the rectangle on the canvas
	Image1->Bitmap->Canvas->DrawRect(MyRect, 30, 60, AllCorners, 100);
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::DrawRectSidesClick(TObject *Sender) {
	// sets the rectangle to be customized and drawn
	TRectF MyRect(50, 40, 200, 270);
	Image1->Bitmap->Canvas->BeginScene();
	// customizes the rectangle and draws it on the canvas
	Image1->Bitmap->Canvas->DrawRectSides(MyRect, 50, 20, AllCorners, 40,
		AllSides, TCornerType(1));
	Image1->Bitmap->Canvas->EndScene();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::CreateThumbnailClick(TObject *Sender) {
	// creates the bitmap from where to draw the thumbnail
	TBitmap* MyBitmap = new TBitmap("bitmap1.png");
	// draws a thumbnail with the given sizes
	Image1->Bitmap = MyBitmap->CreateThumbnail(300,300);
}
// ---------------------------------------------------------------------------

Uses

See Also

Personal tools
Assistance