如何在代码中增加控制台的大小

时间:2018-12-13 19:59:33

标签: windows console lazarus freepascal

我正在使用lazarus IDE v1.8.4在pascal中编写一些代码,因为这个问题说我需要能够在代码中编辑控制台大小,所以我最好还需要获得他们可以拥有的最大控制台宽度。如果您知道如何使用,也请让我知道您的使用方式。谢谢!

2 个答案:

答案 0 :(得分:3)

假设您要定位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.


并且不要忘记添加错误检查。

答案 1 :(得分:1)

这对我在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复制而只更改了窗口尺寸。