Creating drawing spaces (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Using the VCL/RTL (C++)

The TCanvas class encapsulates a Windows device context in the VCL. It handles all drawing for both forms, visual containers (such as panels) and the printer object (see Printing (C++)). Using the canvas object, you no longer have to worry about allocating pens, brushes, palettes, and so on — all the allocation and deallocation are handled for you.

TCanvas includes a large number of primitive graphics routines to draw lines, shapes, polygons, fonts, etc. onto any control that contains a canvas. For example, here is a button event handler that draws a line from the upper left corner to the middle of the form and outputs some raw text onto the form:

void __fastcall TForm1::Button1Click(TObject *Sender) {
	Canvas->Pen->Color = clBlue;
	Canvas->MoveTo(10, 10);
	Canvas->LineTo(100, 100);
	Canvas->Brush->Color = clBtnFace;
	Canvas->Font->Name = "Arial";
	Canvas->TextOut(Canvas->PenPos.x, Canvas->PenPos.y,
			"This is the end of the line");
}

The TCanvas object also protects you against common Windows graphics errors, such as restoring device contexts, pens, brushes, and so on to the value they had before the drawing operation. TCanvas is used everywhere in C++Builder that drawing is required or possible, and makes drawing graphics both fail-safe and easy.