Programming C++Builder How to Use Canvas to Draw Graphic Objects such as Lines, Polylines, Shapes, Rectangles, and Polygons in CBuilder?

How to use canvas to draw graphic objects such as lines, polylines, shapes, rectangles, and polygons?

This section shows how to use some common methods to draw graphic objects. It covers:

  1. Drawing lines.
  2. Drawing polylines.
  3. Drawing shapes.
  4. Drawing rounded rectangles.
  5. Drawing polygons.

How to draw lines?

A canvas can draw straight lines and polylines. A straight line is just a line of pixels connecting two points. A polyline is a series of straight lines, connected end-to-end. The canvas draws all lines using its pen.

How to draw the lines?

To draw a straight line on a canvas, use the LineTo method of the canvas. LineTo draws a line from the current pen position to the point you specify and makes the endpoint of the line the current position. The canvas draws the line using its pen.

For example, the following method draws crossed diagonal lines across a form whenever the form is painted: 

void __fastcallTForm1::FormPaint(TObject *Sender)
  Canvas->LineTo(ClientWidth, ClientHeight);
  Canvas->MoveTo(0, ClientHeight);
  Canvas->LineTo(ClientWidth, 0);

How to draw the polylines?

In addition to individual lines, the canvas can also draw polylines, which are groups of any number of connected line segments. To draw a polyline on a canvas, call the Polyline method of the canvas.

The parameter passed to the Polyline method is an array of points. You can think of a polyline as performing a MoveTo on the first point and LineTo on each successive point. For drawing multiple lines, Polyline is faster than using the MoveTo method and the LineTo method because it eliminates a lot of call overhead.

How to draw the shapes?

Canvases have methods for drawing different kinds of shapes. The canvas draws the outline of a shape with its pen, then fills the interior with its brush. The line that forms the border for the shape is controlled by the current Pen object.

This section covers:

  1. Drawing rectangles and ellipses.
  2. Drawing rounded rectangles.
  3. Drawing polygons.

How to draw rectangles and ellipses?

To draw a rectangle or ellipse on a canvas, call the canvas's Rectangle method or Ellipse method, passing the coordinates of a bounding rectangle. The Rectangle method draws the bounding rectangle; Ellipse draws an ellipse that touches all sides of the rectangle.

How to draw rounded rectangles?

To draw a rounded rectangle on a canvas, call the canvas's RoundRect method. The first four parameters passed to RoundRect are a bounding rectangle, just as for the Rectangle method or the Ellipse method. RoundRect takes two more parameters that indicate how to draw the rounded corners.

How to draw polygons?

To draw a polygon with any number of sides on a canvas, call the Polygon method of the canvas. Polygon takes an array of points as its only parameter and connects the points with the pen, then connects the last point to the first to close the polygon. After drawing the lines, Polygon uses the brush to fill the area inside the polygon.

Add comment

Security code

Programming - C++Builder