A short guide to working with the TCanvas class for beginners (2)

WILD

Administrator
Staff member
ADMIN
SELLER
SUPREME
MEMBER
Joined
Jan 21, 2025
Messages
219
Reaction score
637
Deposit
0$
How to draw a square using the LineTo method

Using the LineTo and MoveTo methods, you can draw not only simple lines, but also objects such as a rectangle or a square. The example below shows how this can be done:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Canvas->MoveTo(25,25);// Move the pen position to the point (25,25),
// so that the shape we have drawn is better visible

Form1->Canvas->LineTo(25,200); //Draw a vertical line
// to a point 200 pixels down - the left side of the rectangle

//Since the pen position is now equal to (25,200),
// we don't need to manually change it, so we...

Form1->Canvas->LineTo(200,200); //Draw a horizontal line to the point (200,200)
// - this will be the lower border of the rectangle

Form1->Canvas->LineTo(200,25); //Then we draw a line to the point (200,250),
// thus drawing the right side of the rectangle

Form1->Canvas->LineTo(25,25); // And finally, the last touch - draw a horizontal line
// to the starting point from which we started drawing,

1772318281472.png
In the image, the red lines indicate the direction in which we drew the lines.

However, it is not very convenient to use such techniques every time we need to draw a similar shape, so the TCanvas class provides special methods for drawing various shapes, such as rectangles, ellipses, arcs, and so on.
We will discuss the use of these methods in the following sections...
 
Top Bottom