RAD Studio |  |

Tracking the Origin Point
See Also
Collapse All

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:

Copy 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;

Copy Code

    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

Tracking Movement

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!