Mega Code Archive

 
Categories / Delphi / Games
 

Clearing a console window screen

Question: How do I clear a console screen? Answer: Call the function ClearConsoleScreen as shown below. function ClearConsoleScreen: boolean; const BUFSIZE = 80*25; var Han,Dummy: LongWord; buf: string; coord: TCoord; begin Result := false; Han := GetStdHandle(STD_OUTPUT_HANDLE); if Han <> INVALID_HANDLE_VALUE then begin if SetConsoleTextAttribute(han, FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE) then begin SetLength(buf,BUFSIZE); FillChar(buf, length(buf),' '); if WriteConsole(han,PChar(buf),BUFSIZE,Dummy,nil) then begin coord.X := 0; coord.Y := 0; if SetConsoleCursorPosition(han,coord) then Result := true; end; end; end; end;