Question

I'm trying to draw on a TShape via the Canvas, but, nothing display.

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;        

Thanks for reading

Was it helpful?

Solution

That's because you're calling Refresh method. This method immediately forces the control to repaint. Draw your painting in the OnPaint event method instead and call just that Refresh or Invalidate on that shape object to force it to trigger the OnPaint event:

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;

In your original code you were also trying to draw on quite weird position. Canvas coordinates starts at [0; 0] and goes to [Control.ClientWidth; Control.ClientHeight].

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