Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Spinner in a Delphi Console

I am trying to create a spinner / wait cursor in a Delphi console application. I can do it but I am sure the code can be streamlined / improved significantly. Please forgive the poor code:

Procedure PositionXY( x , y : Integer);
var
 hStdOut: HWND;
 ScreenBufInfo: TConsoleScreenBufferInfo;
 Coord1: TCoord;
 z: Integer;
 Begin
  sleep(100);
  hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
  GetConsoleScreenBufferInfo(hStdOut, ScreenBufInfo);
  Coord1.X := x;
  Coord1.Y := y;
  SetConsoleCursorPosition(hStdOut, Coord1);
 End;

begin
 while True do  begin
 Write('|');
  PositionXY(0,0);
 Write('/');
  PositionXY(0,0);
 Write('-');
  PositionXY(0,0);
 Write('\');
  PositionXY(0,0);
 end;
 ReadLn;
end.

Thanks in advance Paul

like image 961
Paul Heinrich Avatar asked Oct 22 '11 10:10

Paul Heinrich


1 Answers

This might guide you to some optimizations:

Write('|'#8); Sleep(100);
Write('/'#8); Sleep(100);
Write('-'#8); Sleep(100);
Write('\'#8); Sleep(100);

Hint: The #8 is a BackSpace.

like image 86
Uwe Raabe Avatar answered Sep 20 '22 15:09

Uwe Raabe