2

这个问题是我之前题为How to fix this procedure writing a string to the console screen buffer 的帖子的后续问题。

我想在写入任意字符串之前将光标设置到给定的 (x,y) 位置:

GotoXY(x,y)
SendLn('The harder they come...'); 

怎样才能procedure GotoXY(x, y: integer)实施?

4

2 回答 2

6

一个快速的谷歌揭示

设置控制台光标位置

于 2012-03-30T15:54:11.740 回答
3

作为参考,这是我对这个问题的解决方案,基于 JamesB 的帖子(接受的答案):

procedure GotoXY(x, y: Integer);
var
  CursorCoord: _COORD;
begin
  CursorCoord.x := x;
  CursorCoord.y := y;

  SetConsoleCursorPosition(hStdOut, CursorCoord);
end;

编辑:

上面 jamesB 引用的页面还指向另一个有趣的相关资源,即GetConsoleScreenBufferInfo 函数

在控制台屏幕缓冲区中获取光标的列和行坐标也是我的要求的一部分。

以下是我根据引用的资源编写的 2 个 Delphi 函数:

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;
于 2012-03-30T16:35:29.377 回答