【问题标题】:How can I increase the size of the console inside the code如何在代码中增加控制台的大小
【发布时间】:2018-12-13 19:59:33
【问题描述】:

我正在使用 lazarus IDE v1.8.4 在 pascal 中编写一些代码,因为问题说我需要能够在代码中编辑控制台大小,我最好还需要获得他们可以拥有的最大可能控制台宽度.如果你知道怎么做,也请告诉我你的用途……使用过。谢谢!

【问题讨论】:

  • 你看到这个Q&A了吗?它适用于 Delphi,但应该在 Lazarus/FPC 中工作。
  • 我试过了,但没有运气,我可能会错过它的用途。我会检查一下。
  • 我试过了,但没有运气 try(代码)应该在您的帖子中以显示您所做的事情。如果您在询问支持多个平台的编译器时包含有关您正在使用的操作系统的信息,这也会有所帮助 - console 可以指 Windows 控制台、*nix 终端或游戏设备.
  • 感谢您的建议,我可能应该想到添加它,特别是考虑到问题实际上只是我在使用中缺少窗口

标签: windows console lazarus freepascal


【解决方案1】:

假设您的目标是 Windows:

此时控制台的窗口应该按照您的设置定位。然而,在我的测试中,虽然窗口符合尺寸要求,但该位置被忽略了。

在这种情况下使用任何 API 函数来移动窗口,以下示例使用 SetWindowPos。我必须声明GetConsoleWindow,因为它没有在 Lazarus 1.6 中声明。


program Project1;

{$APPTYPE CONSOLE}

uses
  windows;

function GetConsoleWindow: HWND; stdcall external 'kernel32';

var
  Con: THandle;
  Size: TCoord;
  Rect: TSmallRect;
  Wnd: HWND;
begin
  Con := GetStdHandle(STD_OUTPUT_HANDLE);
  Size := GetLargestConsoleWindowSize(Con);

  SetConsoleScreenBufferSize(Con, Size);

  Rect.Left := -10;
  Rect.Top := -10;
  Rect.Right := Size.X - 11;
  Rect.Bottom := Size.Y - 11;
  SetConsoleWindowInfo(Con, True, Rect);

  Wnd := GetConsoleWindow;
  SetWindowPos(Wnd, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);

  Readln;
end.


别忘了添加错误检查。

【讨论】:

  • 优秀的答案。作为参考,Delphi 10.3 Rio 也没有在WinAPI.Windows 中包含GetConsoleWindow
  • 我通常将缓冲区设置为比窗口大很多的大小,尤其是在垂直维度上(很多很多行,比如 3000 或 4000)。
  • 太棒了,我没有意识到我需要使用 windows,非常感谢,我终于可以继续我的项目了。
  • FPC 3.0.4 因此当前的 Lazarus 版本应该支持 getconsolewindow。
【解决方案2】:

这对我在 Win10Pro 上的 Lazarus 来说似乎工作正常。

program ResizeConsoleWin;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows;

procedure SetConsoleWindowSize;
var
  Rect: TSmallRect;
  Coord: TCoord;
begin
  Rect.Left := 1;
  Rect.Top := 1;
  Rect.Right := 300;  // notice horiz scroll bar once the following executes
  Rect.Bottom := 30;
  Coord.X := Rect.Right + 1 - Rect.Left;
  Coord.y := Rect.Bottom + 1 - Rect.Top;
  SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), Coord);
  SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), True, Rect);
end;

begin
  SetConsoleWindowSize;
  readln;
end.

它是从this answer 复制而来的,只是改变了窗口尺寸。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多