1

我试图通过画布在 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;        

谢谢阅读

4

1 回答 1

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]

于 2013-09-19T22:17:55.557 回答