GotoXY implementation
-
28-05-2021 - |
Question
This question is a follow-up one to my prior post entitled How to fix this procedure writing a string to the console screen buffer.
I want to set the cursor to a given (x,y) position prior to writing an arbitrary string:
GotoXY(x,y)
SendLn('The harder they come...');
How can be procedure GotoXY(x, y: integer)
implemented ?
Solution
A quick google reveals
OTHER TIPS
For reference, this is my solution to the question, based on JamesB's post (the accepted answer):
procedure GotoXY(x, y: Integer);
var
CursorCoord: _COORD;
begin
CursorCoord.x := x;
CursorCoord.y := y;
SetConsoleCursorPosition(hStdOut, CursorCoord);
end;
Edit:
The page refered by jamesB above also points to another interesting related resource, namely GetConsoleScreenBufferInfo function.
Getting the column and row coordinates of the cursor in the console screen buffer is also part of my requirements.
Here are the 2 Delphi functions I've written based on the cited resource:
var
Buffer: _Console_Screen_Buffer_Info;
...
function WhereX: Integer;
begin
GetConsoleScreenBufferInfo(hStdOut,Buffer);
//
Result:=Buffer.dwCursorPosition.X;
end;
function WhereY: Integer;
begin
GetConsoleScreenBufferInfo(hStdOut,Buffer);
//
Result:=Buffer.dwCursorPosition.Y;
end;