【问题标题】:GotoXY implementationGotoXY 实现
【发布时间】:2012-03-30 15:43:03
【问题描述】:

这个问题是我之前题为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)如何实现?

【问题讨论】:

  • 问题是您为什么使用WriteFile 写入控制台。使用控制台 API,例如WriteConsole 和朋友们。
  • @David Heffernan:谢谢:len:=Length(s); WriteConsole(hStdOut, @s[1],len,len,nil);完成这项工作。
  • 请记住,如果标准输出被重定向到一个文件,那么 WriteConsole 将会失败。
  • 幸运的是,这不是我的要求。
  • 或者更简单,获取JEDI JCLs JCLConsole,并使用JCLScreenBuffer.Write(Text, X, Y, Attributes) 方法。 :) Attributes 默认为 nil,因此您可以排除它以使用默认文本和背景颜色。

标签: delphi winapi delphi-xe windows-console


【解决方案1】:

快速谷歌揭示

SetConsoleCursorPosition

【讨论】:

  • 谢谢你指点我。确实,在 Windows.pas 单元中有对它的引用,我想念它。
【解决方案2】:

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

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

  SetConsoleCursorPosition(hStdOut, CursorCoord);
end;

编辑:

上面jamesB引用的page也指向另一个有趣的相关资源,即GetConsoleScreenBufferInfo function

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

这是我根据引用的资源编写的 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;

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多