Mirror

Draw a line from the mouse cursor to a fixed point on a form (Views: 100)


Problem/Question/Abstract:

How to draw a line from the mouse cursor to a fixed point on a form

Answer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    fOldX, fOldY: Integer;
    fLineDrawn: Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  fLineDrawn := false;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

  procedure DrawLine(Color: TColor);
  begin
    Canvas.Pen.Color := Color;
    Canvas.MoveTo(fOldX, fOldY);
    Canvas.LineTo(100, 100);
  end;

begin
  if fLineDrawn then
    DrawLine(Color);
  fOldX := X;
  fOldY := Y;
  DrawLine(clRed);
  fLineDrawn := true;
end;

end.

<< Back to main page