How to move lines on desktop

How to move lines on desktop

How to move lines on desktop

This Delphi graphic tutorial shows how to generate randomly bouncing lines on the desktop using transparent form technique. This program can be modifying to work as a screen saver.

DownloadDownload This Tutorials

Bookmark:

How to move lines on desktop

To create transparent background form set Brush Style to Clear mode. Coordinates for two lines creates randomly using Random function and using screen width and height. First we create a record type for line which holds information about the current coordinate and previous coordinates. We need previous coordinates to erase the previous line before drawing new line, by doing this user will feel lines are actually moving. To draw lines we use canvas LineTo and MoveTo functions.

Move lines on desktop

First assigned following global variables. In here Lines array hold the coordinates information for the lines.

type
     TLines = Record
          OldX1, OldY1, OldX2, OldY2 : Integer;
          Vx1, Vy1, Vx2, Vy2 : Real;
End;

private
     { Private declarations }
     MaxX, MaxY, MaxLines : Integer;
     Lines : Array [1..20] Of TLines;
     FirstDraw : Boolean;

On form create, initialize all the variables, initial line coordinates, line width, transparent form background, no. of moving lines ...etc.

procedure TForm1.FormCreate(Sender: TObject);
Var
     I : Integer;
begin
     { Set transparent background }
     Brush.Style:=bsClear;
     MaxLines:=2;
     MaxX:=Screen.Width;
     MaxY:=Screen.Height;

     { Setup initial coordinates & velocities }
     For I:=1 To MaxLines Do
     Begin
          Lines[I].OldX1:=Random(MaxX);
          Lines[I].OldX2:=Random(MaxX);
          Lines[I].OldY1:=Random(MaxY);
          Lines[I].OldY2:=Random(MaxY);
          Lines[I].Vx1:=Random(10)+5;
          Lines[I].Vx2:=Random(10)+5;
          Lines[I].Vy1:=Random(10)+5;
          Lines[I].Vy2:=Random(10)+5;
     End;
     FirstDraw:=True;

     { Set line width }
     Canvas.Pen.Width:=20;
     Canvas.Pen.Mode:=pmNotXor;
end;

This one does all the drawing and erasing previous lines.

procedure TForm1.UpdateLines(Sender: TObject);
Var
     I, X1, Y1, X2, Y2 : Integer;
     a : Real;
begin
     If Not FirstDraw Then
     For I:=1 to MaxLines Do
     Begin
          { Erase previous lines }
          Canvas.MoveTo(Lines[I].OldX1,Lines[I].OldY1);
          Canvas.LineTo(Lines[I].OldX2,Lines[I].OldY2);
     End;

     { Set acceleration }
     a:=1;

     For I:=1 To MaxLines Do
     Begin
          { Set new coordinates }
          X1:=Lines[I].OldX1+Round(Lines[I].Vx1+0.5*a*2);
          Y1:=Lines[I].OldY1+Round(Lines[I].Vy1+0.5*a*2);
          X2:=Lines[I].OldX2+Round(Lines[I].Vx2+0.5*a*2);
          Y2:=Lines[I].OldY2+Round(Lines[I].Vy2+0.5*a*2);

          { Check velocity }
          If (X1>=MaxX) Or (X1<=0) Then
               Lines[I].Vx1:=-1*Lines[I].Vx1;
          If (X2>=MaxX) Or (X2<=0) Then
               Lines[I].Vx2:=-1*Lines[I].Vx2;
          If (Y1>=MaxY) Or (Y1<=0) Then
               Lines[I].Vy1:=-1*Lines[I].Vy1;
          If (Y2>=MaxY) Or (Y2<=0) Then
               Lines[I].Vy2:=-1*Lines[I].Vy2;

          { Check coodinates }
          If X1<=0 Then
               X1:=0;
          If X1>=MaxX Then
               X1:=MaxX;
          If X2<=0 Then
               X2:=0;
          If X2>=MaxX Then
               X2:=MaxX;
          If Y1<=0 Then
               Y1:=0;
          If Y1>=MaxY Then
               Y1:=MaxY;
          If Y2<=0 Then
               Y2:=0;
          If Y2>=MaxY Then
               Y2:=MaxY;

          Canvas.MoveTo(X1,Y1);
          Canvas.LineTo(X2,Y2);
          Lines[I].OldX1:=X1;
          Lines[I].OldY1:=Y1;
          Lines[I].OldX2:=X2;
          Lines[I].OldY2:=Y2;
     End;

     FirstDraw:=False;
end;

Use system time component to call above procedure.

procedure TForm1.Timer1Timer(Sender: TObject);
begin
     UpdateLines(Self);
end;

In order to exit from this program, I use On Key Down event to close the program.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
     Close;
end;

Download This Delphi Tutorials.

Download materials for this article (Delphi - Tutorials)

Download - move-lines-on-desktop.zipmove-lines-on-desktop.zip
       File size: 8 KB, File type: zip
       Total downloads: 319, Upload date: February 11 - 2009

Leave a comment