Question

I am trying to draw/display a geometric shape on a Delphi form given a list lines and arcs at a specific X and Y (Cartesian).

Example:

-Line X0Y0 to X10Y0
-Line X10Y0 to X10Y10
-Line X10Y10 to X0Y10
-Line X0Y10 to X0Y0
-Arc/Circle at X5Y5 diameter of 1

Would draw a 10x10 square with a 1 unit diameter hole in the center. How can I draw this on a form?

I am trying to use this article has a referece, but is there are better ways to do this? http://docwiki.embarcadero.com/CodeExamples/XE4/en/FMXTCanvasDrawFunctions_(Delphi)

Was it helpful?

Solution

In a new VCL Form application (File->New->VCL Form Application), drop a TButton in the middle of the form, double-click it to create a TForm1.Button1Click event handler, and use this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  OldBrushColor, OldPenColor: TColor;
begin
  // I've enlarged the size of the rectangle (box)
  // to 20 x 20 for illustration purposes.
  OldBrushColor := Self.Canvas.Brush.Color;
  Self.Canvas.Brush.Color := clBlack;
  Self.Canvas.Rectangle(10, 10, 30, 30);
  Self.Canvas.Brush.Color := OldBrushColor;
  Self.Canvas.Ellipse(11, 11, 29, 29);

  // Alternative using MoveTo/LineTo and
  // changing pen color
  OldPenColor := Self.Canvas.Pen.Color;
  Self.Canvas.Pen.Color := clRed;
  Self.Canvas.MoveTo(30, 10);
  Self.Canvas.LineTo(50, 10);
  Self.Canvas.MoveTo(50, 10);
  Self.Canvas.LineTo(50, 30);
  Self.Canvas.MoveTo(50, 30);
  Self.Canvas.LineTo(30, 30);
  Self.Canvas.MoveTo(30, 30);
  Self.Canvas.LineTo(30, 10);
  Self.Canvas.Ellipse(31, 11, 49, 29);

  Self.Canvas.Pen.Color := OldPenColor;

end;

Sample of the above:

Screen capture image

You can find other TCanvas drawing methods (such as Arc, Chord, and the combination of MoveTo and LineTo in the documentation. (The link is for XE4's docs, but the Delphi 2006 documentation should have the info as well.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top