Show: Delphi C++
Display Preferences

Tracking the Origin Point

From RAD Studio
Jump to: navigation, search

Go Up to Refining Line Drawing


When drawing lines, track the point where the line starts with the Origin field. Origin must be set to the point where the mouse-down event occurs, so the mouse-up event handler can use Origin to place the beginning of the line, as in this code:

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Drawing := True;
  Canvas.MoveTo(X, Y);
  Origin := Point(X, Y); { record where the line starts }
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Canvas.MoveTo(Origin.X, Origin.Y); { move pen to starting point }
  Canvas.LineTo(X, Y);
  Drawing := False;
end;
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y) {
	Drawing = true; // set the Drawing flag
	Canvas->MoveTo(X, Y); // set pen position
	Origin = Point(X, Y); // record where the line starts
}

void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y) {
	Canvas->MoveTo(Origin.x, Origin.y); // move pen to starting point
	Canvas->LineTo(X, Y); // draw line from PenPos to (X, Y)
	Drawing = false; // clear the Drawing flag
}

Those changes get the application to draw the final line again, but they do not draw any intermediate actions-the application does not yet support "rubber banding."

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance