Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GotoXY implementation

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 ?

like image 255
menjaraz Avatar asked Jun 27 '26 10:06

menjaraz


2 Answers

A quick google reveals

SetConsoleCursorPosition

like image 160
James Barrass Avatar answered Jun 30 '26 01:06

James Barrass


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;
like image 30
menjaraz Avatar answered Jun 30 '26 00:06

menjaraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!