Create Spinner in Delphi Console

I am trying to create a spinner / wait pointer in a Delphi console application. I can do this, but I'm sure the code can be greatly simplified / improved. Please forgive the bad 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

+6
source share
3 answers

This can help you optimize:

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

Hint: # 8 is BackSpace.

+9
source

My decision after answers and feedback.
Thanks Uwe and Christopher

 const Frame: array[0..3] of char = ('|','/','-','\'); var i : Integer; begin while True do begin for i := 0 to Length(Frame)-1 do begin Write(Frame[i]+#8); Sleep(100); end; ///do something end; ReadLn; end. 

Choose this option to make it easier to change characters. Ie: ASCII Spinners Cooler?

+2
source

Using my code, I’ll change it a bit,

 Procedure WriteXY( x , y : Integer, s : string); var hStdOut: HWND; ScreenBufInfo: TConsoleScreenBufferInfo; Coord1: TCoord; Begin hStdOut := GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdOut, ScreenBufInfo); Coord1.X := x; Coord1.Y := y; Write(s); SetConsoleCursorPosition(hStdOut, Coord1); End; begin while True do begin WriteXY(0,0,'|'); Sleep(100); WriteXY(0,0,'/'); Sleep(100); WriteXY(0,0,'-'); Sleep(100); WriteXY(0,0,'\'); Sleep(100); end; ReadLn; end. 

This makes WriteXY a little more useful to me than just PositionXY.

+1
source

Source: https://habr.com/ru/post/899858/


All Articles