MYOB Exo Clarity

Hide NavigationShow Navigation

  • Contents
  • Index
  • Search
 
Display results with all search words

 

Drawing on the PaintBox

The PaintBox component acts as a mini canvas that can be drawn on programmatically using the Calc tab. You can place a PaintBox component using the Advanced Components toolbar on the Design tab, then add code to its OnPrint event to create shapes and text that might otherwise be impossible using other report components.

The PaintBox has a single property: Canvas, which supports a variety of methods for adding lines, shapes, text and images to the PaintBox, for example:

procedure PaintBox1OnPrint;
begin
  PaintBox1.Canvas.Brush.Color := clRed;
  PaintBox1.Canvas.Pen.Color := clLime;
  PaintBox1.Canvas.FillRect(0, 10, 50, 90);
  PaintBox1.Canvas.Ellipse(0, 10, 50, 90);
end;

Note: All coordinates are relative to the bounding box of the PaintBox component. Shapes that are drawn outside the bounding box will not appear on the report.

Setting Drawing Properties

The following methods can be used to set the properties of the things you draw on the PaintBox:

  • Pen determines the colour and/or width of the outlines of any shapes you draw.
  • Brush determines the fill colour and/or pattern of any shapes you draw.
  • Font determines the properties of any text you draw.

Colors can be specified as standard colours, e.g. clBlue or clLime (look at an object's Color property on the Design tab to see a dropdown list of available standard colours). You can also specify a colour in RGB values using the RGB() conversion function:

PaintBox1.Canvas.Brush.Color := RGB(97,0,165);

Adding Shapes and Lines to a PaintBox

The following methods can be used to add lines and geometric shapes to a PaintBox.

LineTo

Draws a line with the current Pen from the current pen position to the specified coordinates. The pen position is the last point that a line was drawn to. It defaults to 0,0 if nothing has been drawn yet, and can be set with MoveTo (see below).

PaintBox1.Canvas.Pen.Color := clRed;
PaintBox1.Canvas.MoveTo(10, 10);
PaintBox1.Canvas.LineTo(100, 100);

MoveTo

Moves the pen position to the specified coordinates.

Rectangle

Draws a rectangle with the current Pen, filled with the current Brush.

PaintBox1.Canvas.Rectangle(0, 10, 50, 90);

FrameRect

Draws a rectangle outline with the current Pen.

FillRect

Draws a solid rectangle, filled with the current Brush.

Ellipse

Call Ellipse to draw a circle or ellipse on the canvas. Specify the bounding rectangle by giving the top left point at pixel coordinates (X1, Y1) and the bottom right point at (X2, Y2).  

PaintBox1.Canvas.Ellipse(0, 10, 50, 90);

Arc

Use Arc to draw an elliptically curved line with the current Pen. The arc traverses the perimeter of an ellipse that is bounded by the points (X1,Y1) and (X2,Y2). The arc is drawn following the perimeter of the ellipse counterclockwise from the starting point to the ending point. The starting point is defined by the intersection of the ellipse and a line defined by the center of the ellipse and (X3,Y3). The ending point is defined by the intersection of the ellipse and a line defined by the center of the ellipse and (X4, Y4).

For example, the following code produces the following arc:

procedure PaintBox1OnPrint;
begin
 PaintBox1.Canvas.Pen.Color := clRed;
 PaintBox1.Canvas.Arc(10,10,300,200,200,10,50,200);
end;

The following code shows the bounding boxes and lines that define the same arc:

procedure PaintBox1OnPrint;
begin
 PaintBox1.Canvas.Pen.Color := clGray;
 PaintBox1.Canvas.Rectangle(10,10,300,200);
 PaintBox1.Canvas.Ellipse(10,10,300,200);
 PaintBox1.Canvas.Pen.Color := clBlue;
 PaintBox1.Canvas.MoveTo(155,105);
 PaintBox1.Canvas.LineTo(200,10);
 PaintBox1.Canvas.MoveTo(155,105);
 PaintBox1.Canvas.LineTo(50,200);
 PaintBox1.Canvas.Pen.Color := clRed;
 PaintBox1.Canvas.Arc(10,10,300,200,200,10,50,200);
end;

Adding Text to a PaintBox

The following methods can be used to add text to a PaintBox.

TextWidth/TextHeight

Use to calculate how much space is required to print the specified string in the current font.

TextOut

Outputs the specified string at the specified coordinates in the current Font:

PaintBox1.Canvas.Font.Name := 'Arial';
PaintBox1.Canvas.Font.Height := 15;
PaintBox1.Canvas.TextOut(10, 10, 'Hello, World!');

TextRect

Outputs the specified string at the specified coordinates in the current font, clipped to a rectangle:

PaintBox1.Canvas.Font.Name := 'Arial';
PaintBox1.Canvas.Font.Height := 15;
PaintBox1.Canvas.TextRect(10, 10, 100, 10, 'Hello, World!');

Adding Images to a PaintBox

The following methods can be used to add existing image files to a PaintBox.

Draw

Use to draw an existing image on the canvas at the specified coordinates:

PaintBox1.Canvas.Draw(0, 0, FormLogo.Picture.Graphic);

StretchDraw

Use to draw an existing image on the canvas at the specified coordinates, scaled to a fixed size:

PaintBox1.Canvas.StretchDraw(0, 0, 100, 200, FormLogo.Picture.Graphic);