As you did not provide any code, I made this up quickly. This code is only to demonstrate usage of line drawing instead of repeatedly drawing circles to make a line. Drawings made with this code are not persistent.
If you can draw lines (with a round StrokeCap
) instead of circles, it becomes quite easy as follows:
Save the position of the OnMouseDown
event in a pair of singles
, e.g. as fields of the form:
private
xold, yold: single;
Drawing: boolean; // to indicate that we should be drawing in the `OnMouseMove` event
procedure TForm65.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
Drawing := True;
xold := X; yold := Y;
end;
In the OnMouseMove
event get the xold
, yold
values to two local singles (say xpre
, ypre
) and set xold
, yold
to current X
, Y
. Then draw a line from xpre
, ypre
to current X
, Y
position
procedure TForm65.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
var
xpre, ypre: single;
begin
if Drawing then
begin
Canvas.BeginScene;
try
xpre := xold;
ypre := yold; // fetch previous position
xold := X;
yold := Y; // store current position for next event
Canvas.StrokeThickness := 10;
Canvas.StrokeCap := TStrokeCap.Round;
Canvas.Stroke.Color := TAlphaColorRec.Red;
Canvas.DrawLine(PointF(xpre,ypre), PointF(X, Y), 1); // draw line from prev pos to current
finally
Canvas.EndScene;
end;
end;
end;
Reset Drawing
in the OnMouseUp
event
procedure TForm65.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
Drawing := False;
end;
OnMouseMove()
code. – Tom Brunberg