绘制几何形状

时间:2013-05-22 19:01:04

标签: delphi drawing delphi-2006

我试图在Delphi表格上绘制/显示几何形状,给定一个特定X和Y(笛卡尔)的列表线和弧。

示例:

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

将绘制一个10x10的正方形,中心有一个直径为1个单位的孔。我怎样才能在表格上画这个?

我正在尝试使用这篇文章有一个参考,但有没有更好的方法来做到这一点? http://docwiki.embarcadero.com/CodeExamples/XE4/en/FMXTCanvasDrawFunctions_(Delphi)

1 个答案:

答案 0 :(得分:2)

在新的VCL表单应用程序(File->New->VCL Form Application)中,将TButton放在表单中间,双击它以创建TForm1.Button1Click事件处理程序,并使用此代码:

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;

上述样本:

Screen capture image

您可以在{{3}中找到其他TCanvas绘图方法(例如ArcChord以及MoveToLineTo的组合(。链接适用于XE4的文档,但Delphi 2006文档也应该包含信息。)