TShape Lazarus,什么都没显示

时间:2013-09-19 22:11:04

标签: canvas drawing shape lazarus

我正试图通过画布在TShape上画画,但是没有显示。

procedure TController.DrawGrind;
begin
  ShowMessage('I try do draw something');

  with FView.Shape1 do
  begin
    Canvas.MoveTo(Left, Top);
    Canvas.Pen.Width:= 5;
    Canvas.Pen.Style := psSolid;
    Canvas.Pen.Color:= clRed;
    Canvas.Brush.Color:= clRed;
    Canvas.LineTo(Left, Width);
  end;

  FView.Shape1.Refresh;

end;        

感谢您阅读

1 个答案:

答案 0 :(得分:2)

那是因为你正在调用Refresh方法。此方法立即强制控件重新绘制。在OnPaint事件方法中绘制您的绘画,并在该形状对象上仅调用RefreshInvalidate以强制它触发OnPaint事件:

procedure TController.DrawGrind;
begin
  ShowMessage('I try do draw something');
  // if you use Refresh instead of Invalidate, the control will be forced
  // to repaint itself immediately
  FView.Shape1.Invalidate;
end;

procedure TForm1.Shape1Paint(Sender: TObject);
begin
  Shape1.Canvas.Pen.Width := 5;
  Shape1.Canvas.Pen.Color := clRed;
  Shape1.Canvas.Pen.Style := psSolid;
  Shape1.Canvas.MoveTo(0, 0);
  Shape1.Canvas.LineTo(Shape1.ClientWidth, Shape1.ClientHeight);
end;

在您的原始代码中,您还试图利用相当奇怪的位置。画布坐标从[0; 0]开始,然后转到[Control.ClientWidth; Control.ClientHeight]