الفكرة هي أن تضغط بالزر الإيمن لإنهاء الرسم وتوصيل الشكل ببداية الخط
- قم بعمل التغييرات الآتية
في حدث OnMouseDown ضع الكود التالي
كود :
if Button = mbLeft then
begin
_count := _count + 1;
_Xpos[_count] := x;
_Ypos[_count] := y;
end;
DrawLines(Button);
وفي حدث OnCreate ضع التالي
كود :
DrawLines(mbLeft);
وقم بالتعديل على الاجراء DrawLines ليصبح بالشكل التالي
كود :
procedure TForm1.DrawLines(Button : TMouseButton);
var
i : Integer;
begin
if Button = mbLeft then
begin
img1.Canvas.Brush.Style := bsClear;
img1.Canvas.Brush.Color := clBlack;
img1.Canvas.FillRect(Rect(0,0,600,200));
img1.Canvas.Pen.Width := 2;
img1.Canvas.Pen.Color := clRed;
_Ypos[0] := 100;
for i := 1 to _count do
begin
img1.Canvas.MoveTo(_xpos[i-1],_ypos[i-1]);
img1.Canvas.LineTo(_xpos[i],_ypos[i]);
end;
img1.Canvas.MoveTo(_xpos[_count],_ypos[_count]);
img1.Canvas.LineTo(600 - _xpos[_count + 1],100);
img1.Canvas.Pen.Width := 1;
img1.Canvas.Brush.Color := clYellow;
for i := 1 to _count do
begin
img1.Canvas.Rectangle(_xpos[i]-3,_ypos[i]-3,_xpos[i]+3,_ypos[i]+3);
end;
end
else if Button = mbRight then
begin
Img1.Canvas.MoveTo(_XPos[_Count],_YPos[_Count]);
Img1.Canvas.LineTo(_XPos[1],_YPos[1]);
end;
end;